I have implemented a loader from flutter_spinkit package, the loader appears when users navigate to another page through the navigation drawer, the problem is when user clicks the button that navigate to another page the loader appears and stuck without closing or navigating, here is the code :
onTap: () async {
//Close the navigation Drawer
Navigator.pop(context);
//Show dialog that contains the loader
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return WillPopScope(
onWillPop: () async {
return false;
},
child: SpinKitCircle(
color: Colors.purple,
size: 50.0,
),
);
},
);
//Close the Loader Dialog after 3 seconds (it doesn't close)
await Future.delayed(Duration(seconds: 3), () {
Navigator.pop(context);
});
//Navigate to the next page
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const MyChallenges(),
),
);
},
Console is showing this :
E/flutter ( 6263): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: Null check operator used on a null value
E/flutter ( 6263): #0 StatefulElement.state
package:flutter/…/widgets/framework.dart:4926
E/flutter ( 6263): #1 Navigator.of
package:flutter/…/widgets/navigator.dart:2542
E/flutter ( 6263): #2 Navigator.pop
package:flutter/…/widgets/navigator.dart:2432
E/flutter ( 6263): #3 _NavigationDrawerState.buildMenuItems.<anonymous closure>.<anonymous closure>
package:athaddakapp/screens/navigation_drawer.dart:123
E/flutter ( 6263): #4 new Future.delayed.<anonymous closure> (dart:async/future.dart:423:39)
E/flutter ( 6263): #5 _rootRun (dart:async/zone.dart:1418:47)
E/flutter ( 6263): #6 _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 6263): #7 _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 6263): #8 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 6263): #9 _rootRun (dart:async/zone.dart:1426:13)
E/flutter ( 6263): #10 _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 6263): #11 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1260:23)
E/flutter ( 6263): #12 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 6263): #13 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)
E/flutter ( 6263): #14 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)
E/flutter ( 6263): #15 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
the loader keeps loads and doesn't navigate, ill appreciate any idea. Thank you
CodePudding user response:
Try to use global key in main for context in showDialog and Navigator.pop which is used to close the dialog
CodePudding user response:
Use main widget context
to navigate.
Simply change showDialog
context name
builder: (BuildContext dialogContext) {
or builder: (_) {