Home > other >  FutureBuilder<List<Suggestion>> giving me The operator '[]' isn't defined
FutureBuilder<List<Suggestion>> giving me The operator '[]' isn't defined

Time:02-15

I have the error The operator '[]' isn't defined for the type 'Object' in my code, what I trying to do is a list of address based on the data of the snapshot, using i as the counter of the list items.

The error is on here: snapshot.data![i].

@override
  Widget buildSuggestions(BuildContext context) {
    final request = Provider.of<ClientRequest>(context, listen: false);
    final newLocation =
        Provider.of<CustomerLocationModel>(context, listen: false);

    return FutureBuilder(
      future: query == "" ? null : apiClient?.fetchSuggestions(query),
      builder: (context, snapshot) => query == ""
          ? const Center(
              child:
                  Text('Ingresa tu dirección', style: TextStyle(fontSize: 20)))
          : snapshot.hasData
              ? ListView.builder(
                  itemBuilder: (context, i) => ListTile(
                    title: Text((snapshot.data![i]) as Suggestion.description),
                    onTap: () async {
                      var addresses = await Geocoder.google(
                              'XYZ')
                          .findAddressesFromQuery(query);
                      var first = addresses.first;
                      newLocation.customerLocation?.update(
                          'lat', (value) => first.coordinates.latitude,
                          ifAbsent: () => 0.0);
                      newLocation.customerLocation?.update(
                          'long', (value) => first.coordinates.longitude,
                          ifAbsent: () => 0.0);
                      request.setLat = first.coordinates.latitude;
                      request.setLng = first.coordinates.longitude;

                      close(context, snapshot.data![i]);
                      Navigator.pushReplacement(
                        context,
                        PageRouteBuilder(
                          pageBuilder: (context, animation1, animation2) =>
                              const AddNewLocation(),
                          transitionDuration: Duration.zero,
                        ),
                      );
                      print("${request.lat} : ${request.long}");
                      print(request.map);
                    },
                  ),
                  itemCount: snapshot.data.lenght,
                )
              : const Center(
                  child: CircularProgressIndicator(),
                ),
    );
  }

Can anyone help?

CodePudding user response:

Try specifying the type with generic like so:

return FutureBuilder<List<Suggestion>>(
  future: query == "" ? null : apiClient?.fetchSuggestions(query),
  ...
  • Related