Home > Mobile >  The argument type 'List<Plants>?' can't be assigned to the parameter type '
The argument type 'List<Plants>?' can't be assigned to the parameter type '

Time:10-26

i cant run my system because of having an error on users. and also in ListTile Text(plant.name) is error. The argument type 'String?' can't be assigned to the parameter type 'String'. Can someone help me ?

  body: FutureBuilder<List<Plants>>(
      future: PlantsApi.getPlantsLocally(context),
      builder:(context, snapshot) {
        final users = snapshot.data;
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return Center(child: CircularProgressIndicator());
          default:
          if (snapshot.hasError) {
            return Center(child: Text('Some error'),);
          }
          else {
            return buildPlants(users);
          }
        }
      }, 


Widget buildPlants(List<Plants> plants) => 
ListView.builder(
  
  itemCount: plants.length,
  itemBuilder: (context, index) {
    final plant = plants[index];
    return 
    
    ListTile (
      title: Text(plant.name),
    );
  }
  );

my fetch api


class PlantsApi {
  static Future<List<Plants>> getPlantsLocally(BuildContext context) async {
    final assetBundle = DefaultAssetBundle.of(context);
    final data = await assetBundle.loadString('assets/plants.json');
    final body = json.decode(data);

    return body.map<Plants>((e) => Plants.fromJson(e)).toList();
  }
}

this is the sample of the json using data class

class Plants {
  int? id;
  String? name;
  String? image;
  String? descript;
  CharacPlant? charac;
  Scienclass? scienclass;
  String? pestdesease;
  Requirements? requirements;
  CareGuide? careguide;
  String? toxictohuman;
  Plants({
    this.id,
    this.name,
    this.image,
    this.descript,
    this.charac,
    this.scienclass,
    this.pestdesease,
    this.requirements,
    this.careguide,
    this.toxictohuman,
  });

  Plants copyWith({
    int? id,
    String? name,
    String? image,
    String? descript,
    CharacPlant? charac,
    Scienclass? scienclass,
    String? pestdesease,
    Requirements? requirements,
    CareGuide? careguide,
    String? toxictohuman,
  }) {
  

i use data class for the json to convert it to dart

CodePudding user response:

Future builders will always have a nullable data type, i.e. snapshot.data is of type [your_type]? Because of that, the code has to be written as below :

FutureBuilder<List<Plants>>(
      future: PlantsApi.getPlantsLocally(context),
      builder:(context, snapshot) {
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return Center(child: CircularProgressIndicator());
          default:
            if (snapshot.hasData) {
              return buildPlants(snapshot.data!)
            } else {
              return Center(child: Text('Some error'),);
            }
           
        }
      }

Regarding your issue with Text(plant.name), this is because the Text widget required a non-nullable String, but your Plant class's name is a nullable String.

So to resolve the issue you can either give it a default value if its null, i.e. plant.name ?? 'default' or you change the type to non-nullable in your class.

CodePudding user response:

import 'dart:convert';

import 'package:oplantapp/model/careguideData.dart';
import 'package:oplantapp/model/characplant.dart';
import 'package:oplantapp/model/requirementData.dart';
import 'package:oplantapp/model/scienclass.dart';

class Plants {
  int? id;
  String? name;
  String? image;
  String? descript;
  CharacPlant? charac;
  Scienclass? scienclass;
  String? pestdesease;
  Requirements? requirements;
  CareGuide? careguide;
  String? toxictohuman;
  Plants({
    this.id,
    this.name,
    this.image,
    this.descript,
    this.charac,
    this.scienclass,
    this.pestdesease,
    this.requirements,
    this.careguide,
    this.toxictohuman,
  });

  Plants copyWith({
    int? id,
    String? name,
    String? image,
    String? descript,
    CharacPlant? charac,
    Scienclass? scienclass,
    String? pestdesease,
    Requirements? requirements,
    CareGuide? careguide,
    String? toxictohuman,
  }) {
    return Plants(
      id: id ?? this.id,
      name: name ?? this.name,
      image: image ?? this.image,
      descript: descript ?? this.descript,
      charac: charac ?? this.charac,
      scienclass: scienclass ?? this.scienclass,
      pestdesease: pestdesease ?? this.pestdesease,
      requirements: requirements ?? this.requirements,
      careguide: careguide ?? this.careguide,
      toxictohuman: toxictohuman ?? this.toxictohuman,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'name': name,
      'image': image,
      'descript': descript,
      'charac': charac?.toMap(),
      'scienclass': scienclass?.toMap(),
      'pestdesease': pestdesease,
      'requirements': requirements?.toMap(),
      'careguide': careguide?.toMap(),
      'toxictohuman': toxictohuman,
    };
  }

  factory Plants.fromMap(Map<String, dynamic> map) {
    return Plants(
      id: map['id'] != null ? map['id'] : null,
      name: map['name'] != null ? map['name'] : null,
      image: map['image'] != null ? map['image'] : null,
      descript: map['descript'] != null ? map['descript'] : null,
      charac: map['charac'] != null ? CharacPlant.fromMap(map['charac']) : null,
      scienclass: map['scienclass'] != null ? Scienclass.fromMap(map['scienclass']) : null,
      pestdesease: map['pestdesease'] != null ? map['pestdesease'] : null,
      requirements: map['requirements'] != null ? Requirements.fromMap(map['requirements']) : null,
      careguide: map['careguide'] != null ? CareGuide.fromMap(map['careguide']) : null,
      toxictohuman: map['toxictohuman'] != null ? map['toxictohuman'] : null,
    );
  }

  String toJson() => json.encode(toMap());

  factory Plants.fromJson(String source) => Plants.fromMap(json.decode(source));

  @override
  String toString() {
    return 'Plants(id: $id, name: $name, image: $image, descript: $descript, charac: $charac, scienclass: $scienclass, pestdesease: $pestdesease, requirements: $requirements, careguide: $careguide, toxictohuman: $toxictohuman)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
  
    return other is Plants &&
      other.id == id &&
      other.name == name &&
      other.image == image &&
      other.descript == descript &&
      other.charac == charac &&
      other.scienclass == scienclass &&
      other.pestdesease == pestdesease &&
      other.requirements == requirements &&
      other.careguide == careguide &&
      other.toxictohuman == toxictohuman;
  }

  @override
  int get hashCode {
    return id.hashCode ^
      name.hashCode ^
      image.hashCode ^
      descript.hashCode ^
      charac.hashCode ^
      scienclass.hashCode ^
      pestdesease.hashCode ^
      requirements.hashCode ^
      careguide.hashCode ^
      toxictohuman.hashCode;
  }
}

this is my parsing

  • Related