微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

登录后在 Flutter 中使用 provider 更改布局时出现黑屏

如何解决登录后在 Flutter 中使用 provider 更改布局时出现黑屏

我正在使用提供程序来处理 Flutter 应用程序中的用户登录。我创建了一个名为 AccountManager 的类,它扩展了 ChangeNotifier 并更新了一个布尔值,该布尔值告诉我的主 Scaffold 使用两个小部件中的哪一个来绘制其内容

当应用程序启动时,我没有登录,它正确加载了 NotLoggedInLayout(),它有一个登录表单。我输入我的凭据,使用 AccountManager 登录,它反过来调用 notifyListeners() 并告诉我的消费者重绘并更改为 LoggedInLayout()。我可以看到它在短时间内重新绘制到正确的屏幕,但随后黑屏覆盖了所有内容

之后,如果我按“R”重新加载应用程序,它会正确显示 LoggedInLayout() 因为它记住了登录信息。但是,如果我按“r”进行热重载,则会在终端中出现此错误

Performing hot reload...                                               ⢿
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════
The following assertion was thrown building
Navigator-[GlobalObjectKey<NavigatorState>
_WidgetsAppState#1e7c5](dirty,dependencies: [HeroControllerScope,UnmanagedRestorationScope],state: NavigatorState#c9fc1(tickers: tracking 0 tickers)):
'package:Flutter/src/widgets/navigator.dart': Failed assertion: line 5308 pos 12:
'_history.isNotEmpty': is not true.

Either the assertion indicates an error in the framework itself,or we should
provide substantially
more information in this error message to help you determine and fix the
underlying cause.
In either case,please report this assertion by filing a bug on GitHub:
  https://github.com/Flutter/Flutter/issues/new?template=2_bug.md

The relevant error-causing widget was:
  MaterialApp file:///home/bruno/Desarrollo/TinviTo/creators/lib/main.dart:20:14

When the exception was thrown,this was the stack:
#2      NavigatorState.build (package:Flutter/src/widgets/navigator.dart:5308:12)
#3      StatefulElement.build
(package:Flutter/src/widgets/framework.dart:4710:27)
#4      ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4593:15)
#5      StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#6      Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#7      StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#8      Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#9      ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#10     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#11     ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#12     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#13     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#14     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#15     ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#16     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#17     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#18     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#19     ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#20     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#21     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#22     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#23     StatelessElement.update
(package:Flutter/src/widgets/framework.dart:4674:5)
#24     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#25     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#26     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#27     ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#28     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#29     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#30     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#31     StatelessElement.update
(package:Flutter/src/widgets/framework.dart:4674:5)
#32     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#33     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#34     StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#35     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#36     StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#37     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#38     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#39     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#40     ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#41     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#42     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#43     StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#44     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#45     StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#46     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#47     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#48     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#49     StatelessElement.update
(package:Flutter/src/widgets/framework.dart:4674:5)
#50     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#51     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#52     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#53     ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#54     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#55     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#56     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#57     StatelessElement.update
(package:Flutter/src/widgets/framework.dart:4674:5)
#58     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#59     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#60     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#61     ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#62     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#63     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#64     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#65     ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#66     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#67     SingleChildRenderObjectElement.update
(package:Flutter/src/widgets/framework.dart:6114:14)
#68     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#69     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#70     StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#71     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#72     StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#73     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#74     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#75     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#76     ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#77     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#78     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#79     StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#80     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#81     StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#82     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#83     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#84     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#85     ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#86     _InheritednotifierElement.update
(package:Flutter/src/widgets/inherited_notifier.dart:181:11)
#87     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#88     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#89     StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#90     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#91     StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#92     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#93     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#94     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#95     ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#96     Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#97     ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#98     StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#99     Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#100    StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#101    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#102    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#103    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#104    ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#105    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#106    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#107    StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#108    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#109    StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#110    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#111    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#112    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#113    ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#114    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#115    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#116    StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#117    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#118    StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#119    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#120    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#121    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#122    ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#123    _InheritednotifierElement.update
(package:Flutter/src/widgets/inherited_notifier.dart:181:11)
#124    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#125    SingleChildRenderObjectElement.update
(package:Flutter/src/widgets/framework.dart:6114:14)
#126    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#127    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#128    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#129    ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#130    _InheritednotifierElement.update
(package:Flutter/src/widgets/inherited_notifier.dart:181:11)
#131    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#132    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#133    StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#134    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#135    StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#136    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#137    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#138    StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#139    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#140    StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#141    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#142    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#143    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#144    ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#145    _InheritednotifierElement.update
(package:Flutter/src/widgets/inherited_notifier.dart:181:11)
#146    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#147    SingleChildRenderObjectElement.update
(package:Flutter/src/widgets/framework.dart:6114:14)
#148    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#149    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#150    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#151    ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#152    _InheritednotifierElement.update
(package:Flutter/src/widgets/inherited_notifier.dart:181:11)
#153    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#154    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#155    StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#156    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#157    StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#158    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#159    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#160    StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#161    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#162    StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#163    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#164    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#165    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#166    ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#167    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#168    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#169    StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#170    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#171    StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#172    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#173    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#174    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#175    ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#176    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#177    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#178    StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#179    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#180    StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#181    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#182    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#183    StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#184    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#185    StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#186    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#187    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#188    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#189    ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#190    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#191    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#192    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#193    ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#194    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#195    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#196    StatefulElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4766:11)
#197    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#198    StatefulElement.update
(package:Flutter/src/widgets/framework.dart:4798:5)
#199    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#200    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#201    _InheritedProviderScopeElement.performRebuild
(package:provider/src/inherited_provider.dart:456:11)
#202    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#203    ProxyElement.update (package:Flutter/src/widgets/framework.dart:4945:5)
#204    _InheritedProviderScopeElement.update
(package:provider/src/inherited_provider.dart:477:11)
#205    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#206    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#207    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#208    StatelessElement.update
(package:Flutter/src/widgets/framework.dart:4674:5)
#209    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#210    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#211    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#212    StatelessElement.update
(package:Flutter/src/widgets/framework.dart:4674:5)
#213    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#214    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#215    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#216    StatelessElement.update
(package:Flutter/src/widgets/framework.dart:4674:5)
#217    Element.updateChild (package:Flutter/src/widgets/framework.dart:3363:15)
#218    ComponentElement.performRebuild
(package:Flutter/src/widgets/framework.dart:4618:16)
#219    Element.rebuild (package:Flutter/src/widgets/framework.dart:4284:5)
#220    BuildOwner.buildScope
(package:Flutter/src/widgets/framework.dart:2582:33)
#221    WidgetsBinding.drawFrame
(package:Flutter/src/widgets/binding.dart:875:21)
#222    RendererBinding._handlePersistentFrameCallback
(package:Flutter/src/rendering/binding.dart:328:5)
#223    SchedulerBinding._invokeFrameCallback
(package:Flutter/src/scheduler/binding.dart:1144:15)
#224    SchedulerBinding.handleDrawFrame
(package:Flutter/src/scheduler/binding.dart:1082:9)
#225    SchedulerBinding.scheduleWarmUpFrame.<anonymous closure>
(package:Flutter/src/scheduler/binding.dart:865:7)
(elided 6 frames from class _AssertionError,class _RawReceivePortImpl,class
_Timer,and dart:async-patch)

似乎我的提供者不知何故弄乱了导航器,但我似乎找不到原因。

这是我的主要代码

import 'package:creators/layouts/LoggedIn.dart';
import 'package:creators/layouts/NotLoggedIn.dart';
import 'package:creators/providers/AccountManager.dart';
import 'package:Flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider<AccountManager>(
          create: (_) => AccountManager(),),],child: MaterialApp(
        title: 'App Title',theme: ThemeData(
          primarySwatch: Colors.blue,home: MyHomePage(
          title: 'App Title',debugShowCheckedModeBanner: false,);
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key,required this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),body: Consumer<AccountManager>(
        builder: (context,manager,child) {
          bool isLoggedIn = manager.isLoggedIn;
          return (isLoggedIn) ? LoggedInLayout() : NotLoggedInLayout();
        },);
  }
}

这是 AccountManager 类:

import 'dart:convert';
import 'package:creators/models/User.dart';
import 'package:creators/services/AccountService.dart';
import 'package:Flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';

class AccountManager with ChangeNotifier {
  User? _user;

  User? get user => _user;
  bool get isLoggedIn => _user != null;

  AccountManager() {
    // Inicializar variables desde la base de datos
    _initData();
  }

  void _initData() async {
    await this.loadUser();
    notifyListeners();
  }

  Future localLogin({
    required String email,required String password,}) async {
    var result = await AccountService.localLogin(
      email: email,password: password,);
    if (result.statusCode == 200) {
      String sBody = result.body;
      Map<String,dynamic> body = jsonDecode(sBody);
      User user = new User(
        body['name'],body['email'],body['role'],body['token'],);
      _user = user;
      await this.saveUser();
      notifyListeners();
    } else {
      String error = jsonDecode(result.body)['message'] ?? 'Error desconocido.';
      throw Exception(error);
    }
  }

  Future localRegister({
    required String name,required String email,}) async {
    var result = await AccountService.localRegister(
      name: name,email: email,);
      _user = user;
      await this.saveUser();
      notifyListeners();
    } else {
      String error = jsonDecode(result.body)['message'] ?? 'Error desconocido.';
      throw Exception(error);
    }
  }

  saveUser() async {
    if (_user != null) {
      String bodySerialized = jsonEncode(_user!.toJson());
      SharedPreferences prefs = await SharedPreferences.getInstance();
      prefs.setString('user',bodySerialized);
    }
  }

  loadUser() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? sBody = prefs.getString('user');
    if (sBody != null) {
      Map<String,);
      _user = user;
    }
  }

  void deleteUser() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.remove('user');
  }

  void logout() {
    _user = null;
    this.deleteUser();
    notifyListeners();
  }
}

当我重新启动应用程序并且它已登录显示 LoggedInLayout())时,我可以按下注销按钮,它会正确地更改为 NotLoggedInLayout() 而不会出现黑屏。所以似乎错误只在我登录时发生,而不是在我注销时发生。

解决方法

这很尴尬,但我知道发生了什么。这是因为我在登录后从 Navigator 弹出屏幕。我从我的另一个项目中复制了表单,该项目没有使用提供程序更改屏幕,而是使用 Navigator.pop()。所以我的代码看起来像这样:

await accountManager.localLogin(
  email: email,password: password,);
Navigator.pop(context);

因为现在我使用 Provider 和 Consumer 更改了布局,所以不再需要 Navigator.pop()。它正在弹出我的第一个脚手架,因此是黑屏。解决方案是删除 Navigator.pop(context) 行。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。