@@ -1091,7 +1091,8 @@ class DataClassGenerator {
1091
1091
method += ` '${ p . jsonName } ': ` ;
1092
1092
1093
1093
if ( p . isEnum ) {
1094
- method += `${ p . name } .index,\n` ;
1094
+ if ( p . isCollection ) method += `${ p . name } .map((x) => x.index).toList(),\n`
1095
+ else method += `${ p . name } .index,\n` ;
1095
1096
} else if ( p . isCollection ) {
1096
1097
if ( p . isMap || p . listType . isPrimitive ) {
1097
1098
const mapFlag = p . isSet ? ( p . isNullable ? '?' : '' ) + '.toList()' : '' ;
@@ -1123,32 +1124,52 @@ class DataClassGenerator {
1123
1124
* @param {ClassField } prop
1124
1125
*/
1125
1126
function customTypeMapping ( prop , value = null ) {
1127
+ const materialConvertValue = prop . isCollection ? "" :" as int" ;
1126
1128
prop = prop . isCollection ? prop . listType : prop ;
1127
- const addDefault = withDefaultValues && prop . rawType != 'dynamic' ;
1128
- value = value == null ? `${ leftOfValue } map['` + prop . jsonName + "']" : value ;
1129
+ const isAddDefault = withDefaultValues && prop . rawType != 'dynamic' && ! prop . isNullable && prop . isPrimitive ;
1130
+ const addLeftDefault = isAddDefault ? leftOfValue : '' ;
1131
+ const addRightDefault = isAddDefault ? rightOfValue : '' ;
1132
+ value = value == null ? `${ addLeftDefault } map['` + prop . jsonName + "']" : value ;
1129
1133
1130
1134
switch ( prop . type ) {
1131
1135
case 'DateTime' :
1132
- return `DateTime.fromMillisecondsSinceEpoch(${ value } )` ;
1136
+ value = withDefaultValues ? `${ leftOfValue } ${ value } ??0${ rightOfValue } ` : value ;
1137
+ return `DateTime.fromMillisecondsSinceEpoch(${ value } ${ materialConvertValue } )` ;
1133
1138
case 'Color' :
1134
- return `Color(${ value } )` ;
1139
+ value = withDefaultValues ? `${ leftOfValue } ${ value } ??0${ rightOfValue } ` : value ;
1140
+ return `Color(${ value } ${ materialConvertValue } )` ;
1135
1141
case 'IconData' :
1136
- return `IconData(${ value } , fontFamily: 'MaterialIcons')`
1142
+ value = withDefaultValues ? `${ leftOfValue } ${ value } ??0${ rightOfValue } ` : value ;
1143
+ return `IconData(${ value } ${ materialConvertValue } , fontFamily: 'MaterialIcons')`
1137
1144
default :
1138
- return `${ ! prop . isPrimitive ? prop . type + '.fromMap(' : '' } ${ value } ${ ! prop . isPrimitive ? ')' : '' } ${ fromJSON ? ( prop . isDouble ? '.toDouble()' : prop . isInt ? '.toInt()' : '' ) : '' } ${ addDefault && ! prop . isNullable ? ` ?? ${ prop . defValue } ` : '' } ` ;
1145
+ return `${
1146
+ ! prop . isPrimitive ? prop . type + ".fromMap(" : ""
1147
+ } ${ value } ${ ! prop . isPrimitive ? " as Map<String,dynamic>)" : "" } ${
1148
+ fromJSON
1149
+ ? prop . isDouble
1150
+ ? ".toDouble()"
1151
+ : prop . isInt
1152
+ ? ".toInt()"
1153
+ : ""
1154
+ : ""
1155
+ } ${
1156
+ isAddDefault
1157
+ ? ` ?? ${ prop . defValue } ${ addRightDefault } `
1158
+ : ""
1159
+ } `;
1139
1160
}
1140
1161
}
1141
1162
1142
1163
let method = `factory ${ clazz . name } .fromMap(Map<String, dynamic> map) {\n` ;
1143
1164
method += ' return ' + clazz . type + '(\n' ;
1144
-
1165
+
1145
1166
for ( let p of props ) {
1146
1167
method += ` ${ clazz . hasNamedConstructor ? `${ p . name } : ` : '' } ` ;
1147
1168
1148
- const value = `${ leftOfValue } map['${ p . jsonName } ']` ;
1169
+ const value = `map['${ p . jsonName } ']` ;
1170
+
1171
+
1149
1172
1150
-
1151
-
1152
1173
1153
1174
// Add nullable check before serialization
1154
1175
if ( p . isNullable ) {
@@ -1157,27 +1178,44 @@ class DataClassGenerator {
1157
1178
1158
1179
// serialization
1159
1180
if ( p . isEnum ) {
1160
- const defaultValue = withDefaultValues ? ' ?? 0' : '' ;
1161
- method += `${ p . rawType } .values[${ value } ${ defaultValue } ]` ;
1181
+ // List<E>
1182
+ if ( p . isCollection ) {
1183
+ const defaultValue = withDefaultValues ? ' ?? <int>[]' : '' ;
1184
+ method += `${ p . type } .from((${ leftOfValue } ${ value } ${ defaultValue } ${ rightOfValue } as List<int>).map<${ p . listType . rawType } >((x) => ${ p . listType . rawType } .values[x]),)`
1185
+ } else {
1186
+ const defaultValue = withDefaultValues ? ' ?? 0' : '' ;
1187
+ method += `${ p . rawType } .values[${ leftOfValue } ${ value } ${ defaultValue } ${ rightOfValue } as int]` ;
1188
+ }
1189
+
1162
1190
} else if ( p . isCollection ) {
1163
- const defaultValue = withDefaultValues && ! p . isNullable ? ` ?? const ${ p . isList ? '[]' : '{}' } ` : '' ;
1191
+ const defaultValue =
1192
+ withDefaultValues && ! p . isNullable && p . isPrimitive
1193
+ ? ` ?? const <${ p . listType . rawType } >${
1194
+ p . isList ? "[]" : "{}"
1195
+ } )`
1196
+ : "" ;
1164
1197
1165
1198
method += `${ p . type } .from(` ;
1199
+ /// List<String>.from(map['allowed'] ?? const <String>[] as List<String>),
1166
1200
if ( p . isPrimitive ) {
1167
- method += `${ value } ${ defaultValue } )` ;
1201
+ method += `( ${ value } ${ defaultValue } as ${ p . type } )` ;
1168
1202
} else {
1169
- method += `${ value } .map((x) => ${ customTypeMapping ( p , 'x' ) } ) ${ defaultValue } )` ;
1203
+ method += `( ${ value } as List<int>) .map< ${ p . listType . rawType } > ((x) => ${ customTypeMapping ( p , 'x' ) } ,), ${ defaultValue } )` ;
1170
1204
}
1205
+ /// (map['name'] ?? '') as String
1171
1206
} else {
1172
- method += customTypeMapping ( p ) ;
1207
+ if ( p . isPrimitive )
1208
+ method += customTypeMapping ( p ) + ` as ${ p . type } ` ;
1209
+ else
1210
+ method += customTypeMapping ( p ) ;
1173
1211
}
1174
1212
1175
1213
// end nullable check if field is nullable
1176
1214
if ( p . isNullable ) {
1177
1215
method += " : null" ;
1178
1216
}
1179
1217
1180
- method += `${ rightOfValue } as ${ p . type } ,\n` ;
1218
+ method += `,\n` ;
1181
1219
1182
1220
const isLast = p . name == props [ props . length - 1 ] . name ;
1183
1221
if ( isLast ) method += ' );\n' ;
@@ -2136,6 +2174,38 @@ function getReplaceEdit(values, imports = null, showLogs = false) {
2136
2174
const uri = getDoc ( ) . uri ;
2137
2175
2138
2176
const noChanges = [ ] ;
2177
+
2178
+ let ignoreComment = readSetting ( "ignoreComment.enabled" ) ;
2179
+ if ( ignoreComment == '' ) ignoreComment = 'public_member_api_docs, sort_constructors_first' ;
2180
+ const lines = [ ] ;
2181
+ const ignores = [ ] ;
2182
+
2183
+ const text = getDocText ( ) ;
2184
+ text . split ( "\n" ) . forEach ( function ( line ) {
2185
+ if ( line . includes ( "// ignore_for_file:" ) ) lines . push ( line ) ;
2186
+ } ) ;
2187
+ let isIncluding ;
2188
+ ignoreComment . split ( "," ) . forEach ( ( ignore ) => {
2189
+ ignore . trim ( ) ;
2190
+
2191
+ isIncluding = false ;
2192
+ lines . forEach ( ( line ) => {
2193
+ if ( line . includes ( ignore ) ) {
2194
+ isIncluding = true ;
2195
+ }
2196
+ } ) ;
2197
+ for ( let line in lines ) {
2198
+ }
2199
+ if ( ! isIncluding ) ignores . push ( ignore ) ;
2200
+ } ) ;
2201
+ if ( ignores . length > 0 )
2202
+ edit . insert (
2203
+ uri ,
2204
+ new vscode . Position ( 0 , 0 ) ,
2205
+ `// ignore_for_file: ${ ignores . join ( "," ) } \n`
2206
+ ) ;
2207
+
2208
+
2139
2209
for ( var i = clazzes . length - 1 ; i >= 0 ; i -- ) {
2140
2210
const clazz = clazzes [ i ] ;
2141
2211
0 commit comments