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

.update not working

mychaelgo opened this issue · comments

Lib version : progress_dialog: ^1.2.0

Sample code:

void _onSavePressed() async {
    try {
      _progressDialog = ProgressDialog(context, isDismissible: false);
      _progressDialog.show();

      if (profilePictureFile != null) {
        _progressDialog.update(
          message: 'Uploading profile picture',
        );
        await _userRepository.uploadProfilePicture(profilePictureFile);
      }

      _user.fullName = _nameController.text;
      _user.phoneNumber = _phoneController.text;
      _user.email = _emailController.text;
      _progressDialog.update(
        message: 'Updating user data',
      );
      await _userRepository.editUserInFirestore(_user);
      print('_onSavePressed ${_user.id}');
      _progressDialog.hide();
    } catch (err) {
      print('err $err');
      _progressDialog.hide();
    }
  }

Output:

err setState() called in constructor: _BodyState#42577(lifecycle state: created, no widget, not mounted)
This happens when you call setState() on a State object for a widget that hasn't been inserted into the widget tree yet. It is not necessary to call setState() in the constructor, since the state is already assumed to be dirty when it is initially created.

Code is working if i comment _progressDialog.update() function

try writing this line inside the build() method

_progressDialog = ProgressDialog(context, isDismissible: false);

@fayaz07 i've put this code inside build method

_progressDialog = ProgressDialog(context, isDismissible: false);

Error still same

Can you post sample code, where you have encountered this issue?

Here sample code

import 'dart:async';

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

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

  _TestScreenState createState() => _TestScreenState();
}

class _TestScreenState extends State<TestScreen> {
  ProgressDialog _progressDialog;

  int a = 0;

  void _showLoading() {
    try {
      _progressDialog = ProgressDialog(context, isDismissible: true);
      _progressDialog.show();

      // change variable
      setState(() {
        a=1;
      });

      _progressDialog.update(
        message: 'Uploading profile picture',
      );

      // change variable
      setState(() {
        a=2;
      });

      Timer(
        Duration(seconds: 3),
        () {
          print('must hide now');
          _progressDialog.hide();
        },
      );
    } catch (err) {
      print('err $err');
      _progressDialog.hide();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: RaisedButton(
        onPressed: _showLoading,
        child: Text(
          'Show loading',
        ),
      ),
    );
  }
}

the problem maybe we can't setState if progress dialog is updating

there is a check, if the progress dialog is showing, only then setState will be called

so what is the solution?

I haven't checked your code, I will update to you after checking. Thank You

import 'dart:async';

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

void main(){
  runApp(MaterialApp(home: TestScreen()));
}

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

  _TestScreenState createState() => _TestScreenState();
}

class _TestScreenState extends State<TestScreen> {
  ProgressDialog _progressDialog;

  int a = 0;

  void _showLoading() async{
    try {
      _progressDialog.show();

      await Future.delayed(Duration(seconds: 2));

      // change variable
      setState(() {
        a=1;
      });

      _progressDialog.update(
        message: 'Uploading profile picture',
      );

      // change variable
      setState(() {
        a=2;
      });

//      Timer(
//        Duration(seconds: 3),
//            () {
//          print('must hide now');
//          _progressDialog.hide();
//        },
//      );
      Future.delayed(Duration(seconds: 3)).whenComplete((){
        _progressDialog.hide().then((value){
          print("Hidden: $value");
        });
      });
    } catch (err) {
      print('err $err');
      //_progressDialog.hide();
    }
  }

  @override
  Widget build(BuildContext context) {
    _progressDialog = ProgressDialog(context, isDismissible: true);
    return Scaffold(
      body: Center(
        child: RaisedButton(
            onPressed: _showLoading,
            child: Text(
              'Show loading',
            ),
          ),
      ),
    );
  }
}

Check this code, this is working perfectly
Demo

I am closing this issue, you must read the logs and configure properly