Skip to content

Instantly share code, notes, and snippets.

@jonahwilliams
Created July 28, 2018 21:41
Show Gist options
  • Select an option

  • Save jonahwilliams/bb280d523bebf01fcc5415bbf2610b0a to your computer and use it in GitHub Desktop.

Select an option

Save jonahwilliams/bb280d523bebf01fcc5415bbf2610b0a to your computer and use it in GitHub Desktop.
example
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';
import 'dart:async';
//void main() => runApp(const Center(child: const Text('Hello, world!', textDirection: TextDirection.ltr)));
void main() {
runApp(new MaterialApp(home: new EditProfileInformation()));
}
class EditProfileInformation extends StatefulWidget {
@override
EditProfileInformationState createState() {
return new EditProfileInformationState();
}
}
class EditProfileInformationState extends State<EditProfileInformation> {
Stream<int> dbCall;
final StreamController _controller = new StreamController<int>();
final myController = TextEditingController();
@override
void initState() {
super.initState();
dbCall = _controller.stream;
myController.addListener(_printLatestValue);
new Future<void>.delayed(const Duration(seconds: 1)).then((_) {
_controller.add(1);
});
}
void _printLatestValue() {
print("Second text field: ${myController.text}");
}
@override
void dispose() {
myController.removeListener(_printLatestValue);
myController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: const Text(
'Edit profile',
),
),
body: new StreamBuilder<int>(
stream: dbCall,
builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
if (snapshot.connectionState == ConnectionState.active) {
return new Container(
child: new Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: myController,
),
),
new Text(snapshot.data.toString()),
],
),
);
} else if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: const CircularProgressIndicator());
} else {
return Center(
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: const <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: const Icon(Icons.error),
),
Text('Error loading data')
],
),
);
}
}),
floatingActionButton: FloatingActionButton(
onPressed: () {
_controller.add(1);
},
child: const Icon(Icons.done),
),
);
}
}
@DarshanGowda0

Copy link
Copy Markdown
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';
import 'dart:async';

//void main() => runApp(const Center(child: const Text('Hello, world!', textDirection: TextDirection.ltr)));

void main() {
  runApp(new MaterialApp(home: new Home()));
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(),
      floatingActionButton: FloatingActionButton(onPressed: () {
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => EditProfileInformation()),
        );
      }),
    );
  }
}

class EditProfileInformation extends StatefulWidget {
  @override
  EditProfileInformationState createState() {
    return new EditProfileInformationState();
  }
}

class EditProfileInformationState extends State<EditProfileInformation> {
  Stream<int> dbCall;
  final StreamController _controller = new StreamController<int>();
  final myController = TextEditingController();

  @override
  void initState() {
    super.initState();
    dbCall = _controller.stream;
    myController.addListener(_printLatestValue);
    new Future<void>.delayed(const Duration(seconds: 1)).then((_) {
      _controller.add(1);
    });
  }

  void _printLatestValue() {
    print("Second text field: ${myController.text}");
  }

  @override
  void dispose() {
    myController.removeListener(_printLatestValue);
    myController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: const Text(
          'Edit profile',
        ),
      ),
      body: new StreamBuilder<int>(
          stream: dbCall,
          builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
            if (snapshot.connectionState == ConnectionState.active) {
              print(snapshot.data);
              return new Container(
                child: new Column(
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: TextField(
                        controller: myController,
                      ),
                    ),
                    new Text(snapshot.data.toString()),
                  ],
                ),
              );
            } else if (snapshot.connectionState == ConnectionState.waiting) {
              return const Center(child: const CircularProgressIndicator());
            } else {
              return Center(
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: const <Widget>[
                    Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: const Icon(Icons.error),
                    ),
                    Text('Error loading data')
                  ],
                ),
              );
            }
          }),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _controller.add(1);
        },
        child: const Icon(Icons.done),
      ),
    );
  }
}

So, I figured this out. When I do this on the first page, it works fine. But, when I navigate to another page and perform the same operation, I'm able to reproduce it. Can you please look into this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment