Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error while appending data to list with custom Model class(Dart)

I am new to dart and I am trying to create a basic inventory app with different types of chemicals.

I am trying to fetch data from firebase, which is getting back to me perfectly, but when I am trying to store it locally with a custom Model Class, its throwing me the following error

type ‘int’ is not a subtype of type ‘String’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is the code for fetching and storing data locally

Future<void> getLoadedData() async {
final url = Uri.parse(
    'https://inventory-db0eb-default-rtdb.asia-southeast1.firebasedatabase.app/chemicalList.json?auth=$authToken');
try {
  final response = await http.get(url);
  final List<ChemModel> _tempChemical = [];

  final _tempChemList = json.decode(response.body) as Map<String, dynamic>;

  _tempChemList.forEach((elementId, value) {
    _tempChemical.add(
      ChemModel(
        id: ' ',
        name: ' ',
        // name: value['name'] ?? "Empty",
        formula: ' ',
        // formula: value['formula'] ?? "Empty",
        description: ' ',
        molWeight: double.parse(value['molWeight']),
        // description: value['description'] ?? "Empty",)
      ),
    );
  });

  _chemicalList = _tempChemical;
  notifyListeners();
} catch (error) {
  print(error);
  rethrow;
}}

This is my model class

class ChemModel with ChangeNotifier {
  String id;
  String name;
  String formula;
  double molWeight;
  String description;

  ChemModel(
      {required this.id,
      required this.name,
      required this.formula,
      this.description = "",
      this.molWeight = 0});
}

I’m not sure where I am going wrong.

>Solution :

model class may be a nuisance if you share a screenshot of the data source I can help more clearly

for exp :
I mean, the value from the data source is string, and if you’re trying to keep it as a int in the model class, you might get this kind of error.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading