Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

android studio - Looking up a deactivated widget's ancestor is unsafe) flutter

this is my splash screen and I provide the future method to navigate automatically to another screen,

class SplashScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Provider.of<DataBaseProvider>(context).getAllEmployees();
    Future.delayed(Duration(seconds: 2)).then((value) {
      Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => ListViewEmployees()));
    });
    return Scaffold(
      body: Container(
        child: Center(
          child: GestureDetector(
            onTap: (){
              Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => ListViewEmployees()));
            },
            child: Text(
              'Hello!' ,
              style: TextStyle(
                color: Colors.deepPurple ,
                fontSize: 18 ,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

and this is the error message

E/flutter ( 8699): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter ( 8699): At this point the state of the widget's element tree is no longer stable.
E/flutter ( 8699): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.
E/flutter ( 8699): #0      Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> (package:flutter/src/widgets/framework.dart:3825:9)
E/flutter ( 8699): #1      _Closure.call (dart:core-patch/function.dart)
E/flutter ( 8699): #2      Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:3839:6)
E/flutter ( 8699): #3      Element.findAncestorStateOfType (package:flutter/src/widgets/framework.dart:3958:12)
E/flutter ( 8699): #4      Navigator.of (package:flutter/src/widgets/navigator.dart:2185:40)
E/flutter ( 8699): #5      SplashScreen.build.<anonymous closure> (package:gsg_sqlitedb/ui/splash_screen.dart:12:17)
E/flutter ( 8699): #6      _rootRunUnary (dart:async/zone.dart:1198:47)
E/flutter ( 8699): #7      _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 8699): #8      _FutureListener.handleValue (dart:async/future_impl.dart:143:18)
E/flutter ( 8699): #9      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:696:45)
E/flutter ( 8699): #10     Future._propagateToListeners (dart:async/future_impl.dart:725:32)
E/flutter ( 8699): #11     Future._complete (dart:async/future_impl.dart:519:7)
E/flutter ( 8699): #12     new Future.delayed.<anonymous closure> (dart:async/future.dart:322:16)
E/flutter ( 8699): #13     _rootRun (dart:async/zone.dart:1182:47)
E/flutter ( 8699): #14     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 8699): #15     _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 8699): #16     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter ( 8699): #17     _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 8699): #18     _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 8699): #19     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
E/flutter ( 8699): #20     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 8699): #21     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
E/flutter ( 8699): #22     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
E/flutter ( 8699): #23     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)

when I delete future delayed, and make the navigate by a button click, the error disappear

SOLVED !!!!!! here Test breaks when using Future.delayed


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...