Flutter Object to Map Or Class to Json

정규호·2023년 12월 13일
0
dependencies:
  flutter:
    sdk: flutter
  json_annotation:^3.1.1
dev_dependencies:
  flutter_test:
    sdk: flutter
  build_runner: ^1.10.11
  json_serializable: ^3.2.5
part 'person.g.dart';
(explicitToJson: true)
class Person{
String name;
String surname;
Person(this.name,this.surname);
//make sure you have the (Just change part of the method name to your class name)
 factory Person.fromJson(Map<String, dynamic> json) =>
      _$PersonFromJson(json);
  Map<String, dynamic> toJson() => _$PersonToJson(this);//Replace 'Person' with your class nam
  }
flutter pub run build_runner build --delete-conflicting-outputs

출처 : How to convert Class object to data structure Map or a List of Maps in Dart?

profile
The Man Who Lift

0개의 댓글