PO JP
PO JP

Reputation: 31

Flutter how to refresh appBar Title after FutureBuilder

Is it possible to refresh appBar title after widget FutureBuilder ? I'd like to set title after FutureBuilder is done

class _SimpleBarChart extends State<SimpleBarChartPage> {
  String _appBarTitle = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text(_appBarTitle)),
        body: Center(child: futureCAGraph()));
  }

  futureCAGraph() {
    return FutureBuilder(
        future: BddAMKGraphCA().getDataGraphCA(_caAnnee),
        builder: (context, AsyncSnapshot<List<dynamic>> snapshot) {
          if (snapshot.hasData) {
            return ListView.builder(
                itemCount: snapshot.data!.length,
                itemBuilder: (context, int currentIndex) {
                  return affGraphCA(context);
                });
          } else if (snapshot.hasError) {
            return Text('${snapshot.error}');
          }
          //return  a circular progress indicator.
          return new CircularProgressIndicator();
        });
  }

Upvotes: 0

Views: 1256

Answers (4)

user18512115
user18512115

Reputation: 11

You Just Have to update Text(_appBarTitle) with Text(snapshot.data[Index].title)

Upvotes: 1

PO JP
PO JP

Reputation: 31

Sorry, after testing appBar's title don't print anything

Widget build(BuildContext context) {
    return FutureBuilder(
        future: BddAMKGraphCA().getDataGraphCA(_caAnnee),
        builder: (context, AsyncSnapshot<List<dynamic>> snapshot) 
          => Scaffold(
            appBar: AppBar(title: Text(_appBarTitle)),
            body: Center(child: (snapshot.hasData) 
             ? ListView.builder(
                itemCount: snapshot.data!.length,
                itemBuilder: (context, int currentIndex) {
                **print('appBar something');**
                  return affGraphCA(context);
                }) 
             : (snapshot.hasError) 
                   ? Text('${snapshot.error}')
                   : //return  a circular progress indicator.
                    CircularProgressIndicator(),
            ),
          )
        );
  }

Upvotes: 0

RaSha
RaSha

Reputation: 1482

It's not a good practice to call setState during the build method. Instead you can move Scaffold inside the builder like this:

  Widget build(BuildContext context) {
    return FutureBuilder(
        future: BddAMKGraphCA().getDataGraphCA(_caAnnee),
        builder: (context, AsyncSnapshot<List<dynamic>> snapshot) 
          => Scaffold(
            appBar: AppBar(title: Text(_appBarTitle)),
            body: Center(child: (snapshot.hasData) 
             ? ListView.builder(
                itemCount: snapshot.data!.length,
                itemBuilder: (context, int currentIndex) {
                  return affGraphCA(context);
                }) 
             : (snapshot.hasError) 
                   ? Text('${snapshot.error}')
                   : //return  a circular progress indicator.
                    CircularProgressIndicator(),
            ),
          )
        );
  }

Upvotes: 0

Ruchit
Ruchit

Reputation: 2700

you can easily update it with setstate() after you get data from future. just assign _appBarTitle in setstate() as shown below,

 setState(() {
  _appBarTitle=//assign your snapshot data
  });

Upvotes: 0

Related Questions