共享首选项Dart - InternalLinkedHashMap'不是“Map”类型的子类型

时间:2018-05-11 06:48:56

标签: dart sharedpreferences flutter

在尝试集成共享首选项时,我遇到了以下问题。我是颤抖/飞镖的初学者。

Unhandled exception:
[        ] E/flutter (24504): type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, Object>' where
[        ] E/flutter (24504):   _InternalLinkedHashMap is from dart:collection
[        ] E/flutter (24504):   Map is from dart:core
[        ] E/flutter (24504):   String is from dart:core
[        ] E/flutter (24504):   Object is from dart:core
[        ] E/flutter (24504): 
[        ] E/flutter (24504): #0      SharedPreferences.getInstance (package:shared_preferences/shared_preferences.dart)
[        ] E/flutter (24504): <asynchronous suspension>
[        ] E/flutter (24504): #1      AuthManager.init (package:apms_timesheet/data/user/auth_manager.dart:25:55)
[        ] E/flutter (24504): <asynchronous suspension>
[        ] E/flutter (24504): #2      _SplashState._init (package:apms_timesheet/ui/splash/splash_screen.dart:36:24)
[        ] E/flutter (24504): <asynchronous suspension>
[        ] E/flutter (24504): #3      _SplashState.initState (package:apms_timesheet/ui/splash/splash_screen.dart:32:5)

来自splash_screen.dart的片段

  final AuthManager _authManager;

  _SplashState(this._authManager);

  @override
  void initState() {
    super.initState();
    _init();
  }

  Future _init() async {
    await _authManager.init();

    String route;
    if (_authManager.loggedIn) {
      route = TimesheetPageView.routeName;
    } else {
      route = LoginPage.routeName;
    }

    Navigator.pushNamedAndRemoveUntil(context, route, (_) => false);

  }
来自auth_manager.dart的

片段

Future init() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String username = prefs.getString(KEY_USERNAME);
    String oauthToken = prefs.getString(KEY_OAUTH_TOKEN);

    if (username == null || oauthToken == null) {
      _loggedIn = false;
      await logout();
    } else {
      _loggedIn = true;
      _username = username;
    }

    _initialized = true;
  }

0 个答案:

没有答案