Umaiz Khan
Umaiz Khan

Reputation: 1227

Flutter Futurebuilder showing error when showing snapshot data value

I am using Future builder in app and its working fine but when data load and when i am showing it in Text widget its showing this error

Class '_InternalLinkedHashMap<String, dynamic>' has no instance getter 'approved_value'.
Receiver: _LinkedHashMap len:31
Tried calling: approved_value

My code

class _ClaimsScreenState extends State<ClaimsScreen> {
  @override
  initState() {
    super.initState();
    doSomeAsyncStuff();
  }

  Future<List> doSomeAsyncStuff() async {
    final storage = new FlutterSecureStorage();
    String value = await storage.read(key: 'health_card');
    print(value);

    String url2 =
        'api.com';

    final response2 = await http.get(url2);
    var Data = json.decode(response2.body);
    print(Data);
    var DisplayData = Data["records"];

    return DisplayData;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('IGI GENERAL INSURANCE'),
      ),
      body: FutureBuilder<List>(
          future: doSomeAsyncStuff(),
          builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
            if (snapshot.hasData) {
              print('ss');
              print(snapshot.data);
              return ListView.builder(
                  itemCount: snapshot.data.length,
                  itemBuilder: (BuildContext context, int index) {
                    return Card(
                      child: Column(
                        children: <Widget>[
                          Row(
                            children: <Widget>[
                              Row(
                                children: <Widget>[
                                  Text('Approved Value:'),
                                  Text(snapshot.data[index].approved_value)
                                ],
                              ),
                              Row(
                                children: <Widget>[
                                  Text('Patient Name:'),
                                  Text(snapshot.data[index].patient_name)
                                ],
                              )
                            ],
                          ),
                          Row(
                            children: <Widget>[
                              Row(
                                children: <Widget>[
                                  Text('Claimed Value:'),
                                  Text(snapshot.data[index].claimed_value)
                                ],
                              ),
                              Row(
                                children: <Widget>[
                                  Text('status:'),
                                  Text(snapshot.data[index].patient_name)
                                ],
                              )
                            ],
                          )
                        ],
                      ),
                    );
                  });
            } else if (snapshot.hasError) {
              return Padding(
                padding: const EdgeInsets.only(top: 16),
                child: Text('Error: ${snapshot.error}'),
              );
            } else {
              return Padding(
                padding: EdgeInsets.only(top: 16),
                child: Text('Awaiting result...'),
              );
            }
          }),
    );
  }
}

I am using listview builder in future builder because the values are in array and need to show all in text widget

I am not sure why its showing this error if remove the value its working fine just when i show the values in Text widget then its showing error.

Upvotes: 0

Views: 940

Answers (1)

Christopher Moore
Christopher Moore

Reputation: 17123

snapshot.data[index] returns a Map. The Map class does not have an approved_value getter. You likely intended to do snapshot.data[index]['approved_value'] instead.

Upvotes: 1

Related Questions