How to fix Error: Non-nullable variable to be used?

twelvell

In Android Studio I am getting these 2 errors when compiling:

lib/connectivity_provider.dart:60:12: Error: Non-nullable variable 'isConnected' must be assigned before it can be used.
    return isConnected;
           ^^^^^^^^^^^
lib/connectivity_provider.dart:12:8: Error: Field '_isOnline' should be initialized because its type 'bool' doesn't allow null.
  bool _isOnline;
       ^^^^^^^^^

What could I have done wrong here with return isConnected;?

import 'dart:async';
import 'dart:io';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';


class ConnectivityProvider with ChangeNotifier {
  Connectivity _connectivity = new Connectivity();

  bool _isOnline;
  bool get isOnline => _isOnline;

  startMonitoring() async {
    await initConnectivity();
    _connectivity.onConnectivityChanged.listen((
        ConnectivityResult result,
        ) async {
      if (result == ConnectivityResult.none) {
        _isOnline = false;
        notifyListeners();
      } else {
        await _updateConnectionStatus().then((bool isConnected) {
          _isOnline = isConnected;
          notifyListeners();
        });
      }
    });
  }

  Future<void> initConnectivity() async {
    try {
      var status = await _connectivity.checkConnectivity();

      if (status == ConnectivityResult.none) {
        _isOnline = false;
        notifyListeners();
      } else {
        _isOnline = true;
        notifyListeners();
      }
    } on PlatformException catch (e) {
      print("PlatformException: " + e.toString());
    }
  }

  Future<bool> _updateConnectionStatus() async {
    bool isConnected;
    try {
      final List<InternetAddress> result =
      await InternetAddress.lookup('google.com');
      if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
        isConnected = true;
      }
    } on SocketException catch (_) {
      isConnected = false;
      //return false;
    }
    return isConnected;
  }
}
Gowtham K K
  1. You need to assign default value as it is not nullable or late init variable.

    bool _isOnline = false; //Here
      bool get isOnline => _isOnline;
    
  2. Since you are returning a future you should have a default value. For example in your case if it comes to non if case in try block without out throwing an execption. It will try to return non assigned value. So it will throw error.

Future<bool> _updateConnectionStatus() async {
    bool isConnected = false; //Here
    try {
        final List < InternetAddress > result =
        await InternetAddress . lookup ('google.com');
        if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
            isConnected = true;
        }
    } on SocketException catch (_) {
        isConnected = false;
        //return false;
    }
    return isConnected;
}

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to fix this error "The non-nullable variable '_mediaQueryData' must be initialized. "

How to fix a nullable expression can't be used as a condition error

How to convert nullable variable to non nullable type?

Error: Non-nullable variable 'mockBuildContext' must be assigned before it can be used

The non-nullable local variable 'title' must be assigned before it can be used. Flutter error

Fix "Variable is used before being assigned" error

How best to convert old Dart code that triggers "The non-nullable variable must be assigned" error?

How to resolve non-nullable error in Flutter?

How to fix 'Variable not used' warning in Powershell, when variable is actually used?

How to fix nullable error in flutter Uint8List

How to fix this array used as initializer error?

How to assign the non-nullable local variable 'user'

How to assign the non-nullable local variable "_msg"

flutter null stafety migration: the non nullable local variable must be assigned before it can be used

non-nullable variable is not initialized

I'm getting non-nullable instance in dart how can I fix it?

How to fix the error undefined variable "$labels"in Prometheus?

How does one fix a "Variable not in scope" error?

PHP error undefined variable notice how to fix?

How to fix the 'variable' was not declared in this scope error?

how fix this error "Undefined variable: products (0)"

how to fix cannot resolve symbol and variable never used, errors

when used Dictionary How can I get the value if it non nullable value type

I have an error like the non-nullable variable 'sessionUser' must be initialized

Convert a nullable to a non-nullable variable in C#/dotnet

What is a Non Static Variable and how do I fix them

How to fix an empty output when using an if statement for a non integer variable?

How to fix 'declared but not used' compiler error in this simple program?

How to fix conversion error in a constructor in one of my used arduino libraries