Esa Juniarto
Esa Juniarto

Reputation: 49

StateError (Bad state: Future already completed) flutter

class MapsView extends GetView<MapsController> {
  MapsView({Key? key}) : super(key: key);

  Completer<GoogleMapController> _controller = Completer();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder<QuerySnapshot<Object?>>(
          stream: controller.getCampus(),
          builder: (context, snap) {
            if (snap.connectionState == ConnectionState.waiting) {
              return Center(
                child: CircularProgressIndicator(),
                // heightFactor: 13,
              );
            }
            if (snap.hasData) {
              var listCampus = snap.data?.docs;
              for (var i = 0; i < listCampus!.length; i++) {
                var detailCampus = listCampus?[i].data()! as Map<String, dynamic>;

                String title = detailCampus["nama"];
                String snipet = detailCampus["categori"];
                double lat = double.parse(detailCampus["latitude"]);
                double long = double.parse(detailCampus["longitude"]);

                LatLng latlong = LatLng(lat, long);
                MarkerId markerId = MarkerId(detailCampus["latitude"] + detailCampus["longitude"]);

                controller.addMarker(markerId, title, snipet, latlong);
              }
              return Stack(
                children: [
                  // _googleMap(context),
                  // _buildContainer(context),
                  Container(
                    height: Get.height,
                    width: Get.width,
                    child: GoogleMap(
                      mapType: MapType.normal,
                      initialCameraPosition: CameraPosition(target: LatLng(-7.6893549, 110.2408421), zoom: 8),
                      onMapCreated: (GoogleMapController controller) {
                        _controller.complete(controller);
                      },
                      markers: Set<Marker>.of(controller.allMarkers),
                    ),
                  ),
                  Align(
                    alignment: Alignment.bottomLeft,
                    child: Container(
                      margin: EdgeInsets.symmetric(vertical: 25),
                      height: 150,
                      child: ListView.builder(
                        scrollDirection: Axis.horizontal,
                        itemCount: listCampus?.length,
                        itemBuilder: ((context, index) {
                          var detailCampus = listCampus?[index].data()! as Map<String, dynamic>;
                          return InkWell(
                            onTap: () async {
                              final GoogleMapController controller = await _controller.future;
                              var lat = double.tryParse(detailCampus['latitude']) ?? 0;
                              var long = double.tryParse(detailCampus['longitude']) ?? 0;
                              controller.animateCamera(
                                CameraUpdate.newCameraPosition(
                                  CameraPosition(target: LatLng(lat, long), zoom: 18, tilt: 50, bearing: 45),
                                ),
                              );
                            },
                            child: Container(
                              width: 350,
                              height: 150,
                              margin: EdgeInsets.only(right: 12),
                              decoration: BoxDecoration(
                                color: Color(0xffF5f5f5),
                                borderRadius: BorderRadius.circular(16),
                              ),
                              child: Row(
                                children: [
                                  Container(
                                    padding: EdgeInsets.all(16),
                                    child: Icon(
                                      Icons.school,
                                      size: 50,
                                    ),
                                  ),
                                  SizedBox(width: 12),
                                  Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      SizedBox(height: 90),
                                      //title
                                      Text(
                                        "${detailCampus["nama"]}",
                                        overflow: TextOverflow.ellipsis,
                                        softWrap: false,
                                        style: GoogleFonts.signika(
                                          fontWeight: FontWeight.bold,
                                          fontSize: 16,
                                        ),
                                      ),
                                      SizedBox(height: 5),
                                      // subtitle
                                      Text(
                                        "${detailCampus["categori"]}",
                                        style: GoogleFonts.signika(
                                          fontWeight: FontWeight.bold,
                                          fontSize: 14,
                                          color: Colors.grey,
                                        ),
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          );
                        }),
                      ),
                    ),
                  ),
                ],
              );
            } else {
              return Center(
                child: Text("Error"),
              );
            }
          }),
    );
  }
}

every time i made a changed in my project this error always appear. i dont know where is the problem. but it does not give any impact in the application. app still running correctly. but this annoying every time i made a change in my program it will appear. please help me to fix this. i also hade some warning with "?" like this [{"The receiver can't be null, so the null-aware operator '?[' is unnecessary.\nTry replacing the operator '?[' with '['.", "source": "dart", "startLineNumber": 32, "startColumn": 46, "endLineNumber": 32, "endColumn": 48 }]

Upvotes: 0

Views: 1353

Answers (1)

Nguyen family
Nguyen family

Reputation: 1010

It's nothing, just a warning. Check out the code that this warning is pointing. That variable can't be null so there's no need to have operator "?". Just carefully investigate that variable, find out why it could be/couldn't be null.

Edit:

Oh I find out. The line above you code listCampus!.length with operator "!" make the compiler surely think listCampus cannot be null. So the line bellow you use listCampus?[i] is not necessary, that's why it gives a warning.

Edit 2:

Check out this related question. Did you complete _controller in anywhere else? Flutter: Google Maps StateError (Bad state: Future already completed)

Upvotes: 1

Related Questions