flutter 一个类在被释放后被使用,一旦你对一个类调用了dispose(),它就不能再被使用

jvlzgdj9  于 2023-03-31  发布在  Flutter
关注(0)|答案(1)|浏览(211)

该错误显示“A MyData was used after being disposed. Once you have called dispose()on a MyData,it can no longer be used.".我无法找到调用dispose方法的位置。任何帮助都将不胜感激。
main.dart

void main() {
  FlutterError.onError = (FlutterErrorDetails details) {
    FlutterError.dumpErrorToConsole(details);
    if (kReleaseMode) exit(1);
  };
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => Auth(),
        ),
//////--------------------
        ChangeNotifierProxyProvider<Auth, MyData>(
          create: (_) => MyData('', 0),
          update: (ctx, auth, _) => MyData(
            auth.token,
            auth.user,
          ),
        ),
//////--------------------
        <other ChangeNotifierProvider s & ChangeNotifierProxyProvider s>
      ],
      child: Consumer<Auth>(
        builder: (ctx, auth, _) => MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData( all theme data ),
          home: auth.isAuth
              ? Home()
              : FutureBuilder(
                  future: auth.autoLogin(),
                  builder: (ctx, authResult) =>
                      authResult.connectionState == ConnectionState.waiting
                          ? Splash()
                          : Login()),
          routes: { <all the routes> },
        ),
      ),
    );
  }
}

home.dart

void didChangeDependencies() {
    if (_spinner == true) {
      Provider.of<MyData>(context, listen: false)
          .refreshData()
          .then((value) {
        if (value == true) {
          _chose =
              Provider.of<MyData>(context, listen: false).data.chose;
        } else {
          setState(() {
            _spinner = false;
          });
          return showDialog(
            context: context,
            builder: (ctx) => AlertBox(),
          );
        }
      }).catchError((error) {
        setState(() {
          _spinner = false;
        });
        return showDialog(
          context: context,
          builder: (ctx) => AlertBox(),
        );
      });

myData.dart

Future<bool> refreshData() async {
    final String _url = 'url here';
    return await http
        .get(_url, headers: {'authorization': '$_auth'}).then((value) async {
   
      if (value.statusCode == 401) {
        Auth().logout();
      }

      if (value.statusCode == 200) {
        final _extractedData = jsonDecode(value.body) as Map<String, dynamic>;
        _data = MyData(data);
        notifyListeners();
        return true;
      } else {
        return false;
      }
    }).catchError((error) {
      throw error;
    });
  }

错误消息(错误未处理时)

[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: A MyData was used after being disposed.
Once you have called dispose() on a MyData, it can no longer be used.
#0      ChangeNotifier._debugAssertNotDisposed.<anonymous closure> (package:flutter/src/foundation/change_notifier.dart:117:9)
#1      ChangeNotifier._debugAssertNotDisposed (package:flutter/src/foundation/change_notifier.dart:123:6)
#2      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:217:12)
#3      MyData.refreshData (package:flutter_app_like4like/providers/myData.dart:210:7)
<asynchronous suspension>
#4      _HomeState.didChangeDependencies (package:flutter_app_like4like/screens/home.dart:43:12)
#5      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4786:12)
#6      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5)
#7      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
#8      Element.updateChild<…>

**我无法找到dispose()。**任何帮助都很有用

wgx48brx

wgx48brx1#

您正在使用的类很可能会在其初始化的屏幕上被释放。这通常会释放应用程序未使用的内存,即当应用程序从一个屏幕导航到另一个屏幕时。与此问题类似,workaround是更新现有的Object,而不是初始化新的Object。

相关问题