0

I want to make a get query with some json data I wrote the class using this web site https://app.quicktype.io/ and it gave me this code

import 'dart:convert';

class Jsonclass {
  final Imei imei;

  Jsonclass({
    this.imei,
  });

  factory Jsonclass.fromJson(Map<String, dynamic> json) => Jsonclass(
        imei: Imei.fromJson(json["imei"]),
      );

  Map<String, dynamic> toJson() => {
        "imei": imei.toJson(),
      };
}

class Imei {
  final String name;
  Test test;

  Imei({
    this.name,
    this.test,
  });

  factory Imei.fromJson(Map<String, dynamic> json) => Imei(
        name: json["name"],
        test: Test.fromJson(json["test"]),
      );

  Map<String, dynamic> toJson() => {
        "name": name,
        "test": test.toJson(),
      };
}

class Test {
  String batterieState;
  String wifiState;
  String pixelState;
  String bluetoothState;
  String gpsState;
  String tactileState;
  String bafleState;
  String microState;
  String vibreurState;
  String camerafrontState;
  String camerabackState;
  String boutonState;
  String usbState;
  String kitState;
  String geroscopeState;
  String empreinteState;
  String captState;
  String greyState;
  String proximiteState;
  String lumiereState;

  Test({
    this.batterieState,
    this.wifiState,
    this.pixelState,
    this.bluetoothState,
    this.gpsState,
    this.tactileState,
    this.bafleState,
    this.microState,
    this.vibreurState,
    this.camerafrontState,
    this.camerabackState,
    this.boutonState,
    this.usbState,
    this.kitState,
    this.geroscopeState,
    this.empreinteState,
    this.captState,
    this.greyState,
    this.proximiteState,
    this.lumiereState,
  });

  factory Test.fromJson(Map<String, dynamic> json) => Test(
        batterieState: json["batterieState"],
        wifiState: json["wifiState"],
        pixelState: json["pixelState"],
        bluetoothState: json["bluetoothState"],
        gpsState: json["gpsState"],
        tactileState: json["tactileState"],
        bafleState: json["bafleState"],
        microState: json["microState"],
        vibreurState: json["vibreurState"],
        camerafrontState: json["camerafrontState"],
        camerabackState: json["camerabackState"],
        boutonState: json["boutonState"],
        usbState: json["usbState"],
        kitState: json["kitState"],
        geroscopeState: json["geroscopeState"],
        empreinteState: json["empreinteState"],
        captState: json["captState"],
        greyState: json["greyState"],
        proximiteState: json["proximiteState"],
        lumiereState: json["lumiereState"],
      );

  Map<String, dynamic> toJson() => {
        "batterieState": batterieState,
        "wifiState": wifiState,
        "pixelState": pixelState,
        "bluetoothState": bluetoothState,
        "gpsState": gpsState,
        "tactileState": tactileState,
        "bafleState": bafleState,
        "microState": microState,
        "vibreurState": vibreurState,
        "camerafrontState": camerafrontState,
        "camerabackState": camerabackState,
        "boutonState": boutonState,
        "usbState": usbState,
        "kitState": kitState,
        "geroscopeState": geroscopeState,
        "empreinteState": empreinteState,
        "captState": captState,
        "greyState": greyState,
        "proximiteState": proximiteState,
        "lumiereState": lumiereState,
      };
}

Then I wrote the get method to display the data; it worked correctly and the server send data but the problem was with displaying data in flutter it gave me this exception

The following assertion was thrown building FutureBuilder>(dirty, state: _FutureBuilderState>#ba8ab):
type '_TypeError' is not a subtype of type 'String'

and this is my code

import 'dart:convert';

void main() => runApp((JsonDataParse()));
class JsonDataParse extends StatefulWidget {
  @override
  _JsonDataParseState createState() => _JsonDataParseState();
}

class _JsonDataParseState extends State<JsonDataParse> {


  Future <List<Jsonclass>> getData() async{
    final response = await http.get('http://192.168.43.24:27017/api/posts');
    if(response.statusCode==200){
      List phones =json.decode(response.body);
      return phones.map((phone)=>new Jsonclass.fromJson(phone)).toList();
    }
    else {
      throw Exception('Failed to load Users');
    }
  }
  void initState(){
    super.initState();  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home:Scaffold(
      appBar: AppBar(
        title: Text('Mes Smartphones'),
      ),
      body: Center(
        child: FutureBuilder<List<Jsonclass>>(
          future: getData(),
          builder: (context,snapshot){
            if(snapshot.hasData){
              List<Jsonclass> phones =snapshot.data;
              return new ListView(
                children:phones.map((phone) =>Column(
                  children: <Widget>[
                     ListTile(
                        subtitle: Text('bonjour${phone.imei.name}'),
                      ),


                  ],
                )).toList(),
              );
            }
            else if(snapshot.hasError){
              return Text(snapshot.error);
            }
            return new CircularProgressIndicator();
          },
        ),
      ),
    )
    );
  }
}

2 Answers 2

1

Your error is because you are trying to put snapshot.error in a Text widget and it is not a String.

You can fix this by converting the snapshot.error to a String. CHeck the code below: It works perfectly:

Text(snapshot.error.toString())

I hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you it works with this problem but i have a new problem tha was no data send to snapshot i mean that there is a data from the server but snapshot.hasdata always return false
Don't forget to mark the answer correct and upvote if it helps you.
0

You may want to try with the method toString(), this helped me a lot in parsing a string to Text Widgets. Hopes this helps

 ListTile(
   subtitle: Text('bonjour${phone.imei.name.toString()}'),
 ),
return Text(snapshot.error.toString());

2 Comments

thank you it works with this problem but i have a new problem tha was no data send to snapshot i mean that there is a data from the server but snapshot.hasdata always return false
@MohamedBorheneDhahri In that case, I suggest you comparing your snapshot with null value something like if(snapshot != null) { //Do your coding after this }. Hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.