1

newbie to Flutter. My code runs but encounters a

The following LateError was thrown building FutureBuilder(dirty, state: _FutureBuilderState#e1a6f): LateInitializationError: Field 'initialPosition' has not been initialized.

The code is to set up a GoogleMap widget that takes initial position from the device. I get the red screen with that error, but after a few seconds the coordinates gets received and proceeds as normal and displays the map and position correctly. Tried future as well but but I get other errors. Is it supposed to be under the FutureBuilder? In a wrapper.dart or my main.dart?

home.dart:

import 'package:flutter/material.dart';
import 'package:something/services/auth.dart';
import 'screens/map.dart';
import 'package:something/services/geolocator_service.dart';

class LakoApp extends StatefulWidget {
  @override


  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<LakoApp> {

  final AuthService _auth = AuthService();
  final _geolocatorService = GeolocatorService();
  late var initialPosition;

  // @override


  Future getInitialPosition <Position>() async {
    initialPosition = await _geolocatorService.getInitialLocation();
    return initialPosition;
  }

  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: FittedBox(
            child: Text('Something something'),
          ),
          actions: <Widget>[
           // irrelevant code
           // .....
           
          
        ],
        body:

        FutureBuilder(

            future: getInitialPosition(),
            builder: (context, snapshot) {
               return Map(initialPosition);
   
            }
        )
    );
  }  
  }
NinFudj
  • 109
  • 1
  • 12

2 Answers2

1

Future Builders are built even before getting the data. So, you should check whether it has data.

 if (snapshot.hasData) {
                return Map(initialPosition); //Or snapshot.data.
              }else{
                return CircularProgressIndicator();
              }

There are other problems here. I will show some further code to improve your own code.

Your method returns a Future of any type receiving a generic parameter called Position. I think you want to use a data type called position for that you need to move <Position> here as right now the way you are writing it is useless for your specific example.

Future<Position> getInitialPosition () async {
    initialPosition = await _geolocatorService.getInitialLocation();
    return initialPosition;
  }

The FutureBuilder can be like this.

FutureBuilder<Position>(
            future: getInitialPosition(),
            builder: (context, snapshot) {
                if (snapshot.hasData) {
                return Map(snapshot.data);
              }else{
                return CircularProgressIndicator(); 
                //Display loading, you may adapt this widget to your interface or use some state management solution
              }
            }
        )
Cavitedev
  • 613
  • 4
  • 17
  • i trying this out but they created more errors. But I'm sure it has data, when I print(snapshot.data) coordinates appear. – NinFudj Jun 17 '21 at 16:02
  • managed to fix the errors except this in the builder: (context, snapshot) parameter. Error: A non-null value must be returned since the return type 'Widget' doesn't allow null. – NinFudj Jun 17 '21 at 16:07
  • 1
    It needs to return a widget like `CircularPorgressIndicator` or `SizedBox` as it needs to build something – Cavitedev Jun 18 '21 at 13:35
1

Edited the code according to suggestions: got rid of the method and variable, because its redundant

body: FutureBuilder <Position> (
        future: _geolocatorService.getInitialLocation(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Map(snapshot.data!);
          }else {
            return Loading();
NinFudj
  • 109
  • 1
  • 12