how to set bool value of checking connectivity when the main() is loaded, and then use it to show which screen

What I have tried is that I used variable outside the method and set it by calling the method, but it didn't work. When I check like _checkConnectivity ? SplashScreen() : WarningScreen() then it shows the error like 'Future' has no instance method 'call'.

import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';
import 'package:algorithm_send_location/pages/home_screen.dart';
import 'package:algorithm_send_location/pages/splash_screen.dart';
import 'package:algorithm_send_location/pages/initial_warning.dart';
import 'package:geolocator/geolocator.dart';

var routes = <String, WidgetBuilder>{
  "/home": (BuildContext context) => HomeScreen(),
};

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

bool accessible = false;

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext buildContext) {
    _checkConnectivity;
    return MaterialApp(
        theme: ThemeData(
            primaryColor: Colors.amber, accentColor: Colors.green[200]),
        debugShowCheckedModeBanner: false,
        home: _checkConnectivity ? SplashScreen() : WarningScreen(),
        routes: routes);
  }
}

get _checkConnectivity async {
  var internetResult = await Connectivity().checkConnectivity();
  bool locationResult = await Geolocator().isLocationServiceEnabled();
  if (internetResult == ConnectivityResult.none || !locationResult) {
    //accessible = false;
    //print(accessible);
    return false;
  } else {
    //accessible = true;
    //print(accessible);
    return true;
  }
}


1 answer

  • answered 2019-10-08 04:47 Dustin Catap

    Use FutureBuilder if you want to use a Future's result for building your UI.

    E.g.

    void main() => runApp(MyApp());
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      Future<bool> _checkConnectivity;
    
      @override
      void initState() {
        _checkConnectivity = _onCheckConnectivity();
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Material App',
          home: _buildHome(),
        );
      }
    
      Widget _buildHome() {
        return FutureBuilder<bool>(
          future: _checkConnectivity,
          builder: (context, snapshot) {
            switch (snapshot.connectionState) {
              case ConnectionState.done:
                final hasConnection = snapshot.data;
    
                return hasConnection ? SplashScreen() : WarningScreen();
    
              default: // Return a loading indicator while checking connection
                return Scaffold(
                  body: Center(
                    child: SizedBox(
                      width: 56.0,
                      height: 56.0,
                      child: CircularProgressIndicator(),
                    ),
                  ),
                );
            }
          },
        );
      }
    
      Future<bool> _onCheckConnectivity() {
        // Async operation here
      }
    }
    

    Note that we need to use a Future variable as the future parameter in FutureBuilder. According to docs:

    FutureBuilder

    The future must have been obtained earlier, e.g. during State.initState, State.didUpdateConfig, or State.didChangeDependencies. It must not be created during the State.build or StatelessWidget.build method call when constructing the FutureBuilder. If the future is created at the same time as the FutureBuilder, then every time the FutureBuilder's parent is rebuilt, the asynchronous task will be restarted.