Skip to content
This repository has been archived by the owner on Nov 24, 2020. It is now read-only.

.update not working #22

Closed
mychaelgo opened this issue Sep 16, 2019 · 9 comments
Closed

.update not working #22

mychaelgo opened this issue Sep 16, 2019 · 9 comments
Labels
waiting for response waiting for the customer to respond with the updated code/comment

Comments

@mychaelgo
Copy link

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

@fayaz07
Copy link
Owner

fayaz07 commented Sep 16, 2019

try writing this line inside the build() method

_progressDialog = ProgressDialog(context, isDismissible: false);

@fayaz07 fayaz07 added the waiting for response waiting for the customer to respond with the updated code/comment label Sep 20, 2019
@mychaelgo
Copy link
Author

@fayaz07 i've put this code inside build method

_progressDialog = ProgressDialog(context, isDismissible: false);

Error still same

@fayaz07
Copy link
Owner

fayaz07 commented Sep 20, 2019

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

@mychaelgo
Copy link
Author

mychaelgo commented Sep 20, 2019

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

@fayaz07
Copy link
Owner

fayaz07 commented Sep 20, 2019

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

@mychaelgo
Copy link
Author

so what is the solution?

@fayaz07
Copy link
Owner

fayaz07 commented Sep 20, 2019

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

@fayaz07
Copy link
Owner

fayaz07 commented Sep 21, 2019

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

@fayaz07
Copy link
Owner

fayaz07 commented Sep 23, 2019

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
waiting for response waiting for the customer to respond with the updated code/comment
Projects
None yet
Development

No branches or pull requests

2 participants