fayaz07 / progress_dialog

A light weight library to easily manage a progress dialog with simple steps whenever you need to do it. You can easily show and hide it.

Home Page:https://fayaz07.github.io/progress_dialog/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crashed under a stateful widget

trueshail opened this issue · comments

Could you please show how could i use it under a stateful widget?

NVM figured out!!

It crashed when using the update command.

Clashes with set state.

Change State method shouldn't be like these? It fixed my problem if I implemented mounted check!

changeState() { if (mounted) { setState(() {}); } }

Can you post the whole code

I tried to use your program under a stateful widget and i added set state when the dialog is show.

Calling Set State when the dialog is open it crashes.

import 'package:flutter/material.dart';
import 'package:progress_dialog/progress_dialog.dart';

ProgressDialog pr;

class MyApp extends StatefulWidget {
  MyApp({Key key}) : super(key: key);

  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var percentage = 0.0;
  @override
  Widget build(BuildContext context) {
    pr = new ProgressDialog(context, ProgressDialogType.Download);
    pr.setMessage('Downloading file...');
    return Scaffold(
      body: Center(
        child: RaisedButton(
            child: Text(
              'Show Dialog',
              style: TextStyle(color: Colors.white),
            ),
            color: Colors.blue,
            onPressed: () {
              pr.show();
              Future.delayed(Duration(seconds: 2)).then((onvalue) {
                percentage = percentage + 30.0;
                print(percentage);
                pr.update(progress: percentage, message: "Please wait...");
              });

              setState(() {});
              
              Future.delayed(Duration(seconds: 2)).then((value) {
                percentage = percentage + 30.0;
                pr.update(progress: percentage, message: "Few more seconds...");
                print(percentage);
              });
              Future.delayed(Duration(seconds: 2)).then((value) {
                percentage = percentage + 30.0;
                pr.update(progress: percentage, message: "Almost done...");
                print(percentage);
              });
              Future.delayed(Duration(seconds: 2)).then((value) {
                pr.hide();
                percentage = 0.0;
              });
            }),
      ),
    );
  }
}

It produces the error below:

I/flutter ( 9356): ProgressDialog shown
I/flutter ( 9356): ProgressDialog message changed: Downloading file...
I/flutter ( 9356): 120.0
I/flutter ( 9356): ProgressDialog message changed:
I/flutter ( 9356): Old Progress: 90.0, New Progress: 120.0
I/flutter ( 9356): Old message: Downloading file..., New Message: Please wait...
E/flutter ( 9356): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'update' was called on null.
E/flutter ( 9356): Receiver: null
E/flutter ( 9356): Tried calling: update()
E/flutter ( 9356): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
E/flutter ( 9356): #1 ProgressDialog.update
E/flutter ( 9356): #2 _MyAppState.build..
E/flutter ( 9356): #3 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter ( 9356): #4 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 9356): #5 _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
E/flutter ( 9356): #6 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
E/flutter ( 9356): #7 Future._propagateToListeners (dart:async/future_impl.dart:668:32)
E/flutter ( 9356): #8 Future._complete (dart:async/future_impl.dart:473:7)
E/flutter ( 9356): #9 new Future.delayed. (dart:async/future.dart:313:16)
E/flutter ( 9356): #10 _rootRun (dart:async/zone.dart:1120:38)
E/flutter ( 9356): #11 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 9356): #12 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter ( 9356): #13 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:963:23)
E/flutter ( 9356): #14 _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 9356): #15 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 9356): #16 _CustomZone.bindCallback. (dart:async/zone.dart:947:23)
E/flutter ( 9356): #17 Timer._createTimer. (dart:async-patch/timer_patch.dart:21:15)
E/flutter ( 9356): #18 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:382:19)
E/flutter ( 9356): #19 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:416:5)
E/flutter ( 9356): #20 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:171:12)
E/flutter ( 9356):
I/flutter ( 9356): ProgressDialog message changed:
I/flutter ( 9356): Old Progress: 120.0, New Progress: 150.0
I/flutter ( 9356): Old message: Please wait..., New Message: Few more seconds...
E/flutter ( 9356): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'update' was called on null.
E/flutter ( 9356): Receiver: null
E/flutter ( 9356): Tried calling: update()
E/flutter ( 9356): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
E/flutter ( 9356): #1 ProgressDialog.update
E/flutter ( 9356): #2 _MyAppState.build..
E/flutter ( 9356): #3 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter ( 9356): #4 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 9356): #5 _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
E/flutter ( 9356): #6 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
E/flutter ( 9356): #7 Future._propagateToListeners (dart:async/future_impl.dart:668:32)
E/flutter ( 9356): #8 Future._complete (dart:async/future_impl.dart:473:7)
E/flutter ( 9356): #9 new Future.delayed. (dart:async/future.dart:313:16)
E/flutter ( 9356): #10 _rootRun (dart:async/zone.dart:1120:38)
E/flutter ( 9356): #11 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 9356): #12 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter ( 9356): #13 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:963:23)
E/flutter ( 9356): #14 _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 9356): #15 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 9356): #16 _CustomZone.bindCallback. (dart:async/zone.dart:947:23)
E/flutter ( 9356): #17 Timer._createTimer. (dart:async-patch/timer_patch.dart:21:15)
E/flutter ( 9356): #18 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:382:19)
E/flutter ( 9356): #19 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:416:5)
E/flutter ( 9356): #20 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:171:12)
E/flutter ( 9356):
I/flutter ( 9356): ProgressDialog message changed:
I/flutter ( 9356): Old Progress: 150.0, New Progress: 180.0
I/flutter ( 9356): Old message: Few more seconds..., New Message: Almost done...
E/flutter ( 9356): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'update' was called on null.
E/flutter ( 9356): Receiver: null
E/flutter ( 9356): Tried calling: update()
E/flutter ( 9356): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
E/flutter ( 9356): #1 ProgressDialog.update
E/flutter ( 9356): #2 _MyAppState.build..
E/flutter ( 9356): #3 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter ( 9356): #4 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 9356): #5 _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
E/flutter ( 9356): #6 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
E/flutter ( 9356): #7 Future._propagateToListeners (dart:async/future_impl.dart:668:32)
E/flutter ( 9356): #8 Future._complete (dart:async/future_impl.dart:473:7)
E/flutter ( 9356): #9 new Future.delayed. (dart:async/future.dart:313:16)
E/flutter ( 9356): #10 _rootRun (dart:async/zone.dart:1120:38)
E/flutter ( 9356): #11 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 9356): #12 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter ( 9356): #13 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:963:23)
E/flutter ( 9356): #14 _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 9356): #15 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 9356): #16 _CustomZone.bindCallback. (dart:async/zone.dart:947:23)
E/flutter ( 9356): #17 Timer._createTimer. (dart:async-patch/timer_patch.dart:21:15)
E/flutter ( 9356): #18 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:382:19)
E/flutter ( 9356): #19 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:416:5)
E/flutter ( 9356): #20 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:171:12)
E/flutter ( 9356):
E/flutter ( 9356): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'ancestorStateOfType' was called on null.
E/flutter ( 9356): Receiver: null
E/flutter ( 9356): Tried calling: ancestorStateOfType(Instance of 'TypeMatcher')
E/flutter ( 9356): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
E/flutter ( 9356): #1 Navigator.of
E/flutter ( 9356): #2 ProgressDialog.hide
E/flutter ( 9356): #3 _MyAppState.build..
E/flutter ( 9356): #4 _rootRunUnary (dart:async/zone.dart:1132:38)
E/flutter ( 9356): #5 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 9356): #6 _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
E/flutter ( 9356): #7 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
E/flutter ( 9356): #8 Future._propagateToListeners (dart:async/future_impl.dart:668:32)
E/flutter ( 9356): #9 Future._complete (dart:async/future_impl.dart:473:7)
E/flutter ( 9356): #10 new Future.delayed. (dart:async/future.dart:313:16)
E/flutter ( 9356): #11 _rootRun (dart:async/zone.dart:1120:38)
E/flutter ( 9356): #12 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 9356): #13 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter ( 9356): #14 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:963:23)
E/flutter ( 9356): #15 _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 9356): #16 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 9356): #17 _CustomZone.bindCallback. (dart:async/zone.dart:947:23)
E/flutter ( 9356): #18 Timer._createTimer. (dart:async-patch/timer_patch.dart:21:15)
E/flutter ( 9356): #19 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:382:19)
E/flutter ( 9356): #20 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:416:5)
E/flutter ( 9356): #21 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:171:12)
E/flutter ( 9356):

you don't have to call setState(() {}); to update the message, it's already a stateful widget and observe there if you can understand, all the future instances will at the same time not one after other, if you would like to run them one after other, you should nest them.

Follow the example https://pub.dev/packages/progress_dialog#-example-tab-

I get that.
The reason i added the setState(() {}); is show that i have a method running to get me the list view and want to update the screen,
That is where it creates a problem.