'The argument type 'String?' can't be assigned to the parameter type 'String'. Shared preferences package

I'm getting an error with Shared Preferences package. I'm using the last version of the package that supports null safety.

    import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';

class Network {
  final String _url = 'http://127.0.0.1:8000/api';
  var token;

  _getToken() async {
    SharedPreferences localStorage = await SharedPreferences.getInstance();
    token = jsonDecode(localStorage.getString('access_token'));
    print(token.toString());
  }

I'm getting this error :

The argument type 'String?' can't be assigned to the parameter type 'String'.


Solution 1:[1]

I fixed it like this :

    token = jsonDecode(localStorage.getString('access_token') as String);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BOUHAMED Iheb