There is a FirstScreen screen and there is a second SecondScreen called from the first screen, in the first screen a web socket is launched, data arrives on it, on the second screen, let's say there is a Text () widget into which you need to write data from the first screen when SecondScreen is already running. Which way to look?
CodePudding user response:
You can manage this issue using state management like provider or bloc
CodePudding user response:
var _channel;
var _screenShow = 1;
class FirstScreen extends StatefulWidget {
FirstScreen();
@override
_FirstScreenState createState() => _FirstScreenState();
}
class _FirstScreenState extends State<FirstScreen> {
void _serverListen() {
_channel.stream.listen((message) {
String res = message.trim();
if (_screenShow == 1) {
// update widjet SecondScreen
} else if (_screenShow == 2) {
// update widjet ThirdScreen
} else if (_screenShow == 3) {
// update widjet FourthScreen
} else {
// update widjet FifthScreen
}
},
onDone: () {
print('ws channel closed');
},
one rror: (error) {
print('ws error $error');
},
);
}
@override
void initState() {
super.initState();
_channel = IOWebSocketChannel.connect(Uri.parse('ws address'));
_serverListen();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('first screen'),
),
body: Text('FirstScreen'),
floatingActionButton: FloatingActionButton(
elevation: 5,
child: Icon(Icons.grid_on,),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
},
),
);
}
// SecondScreen
class SecondScreen extends StatefulWidget {
SecondScreen();
@override
_SecondScreenState createState() => _SecondScreenState();
}
class _SecondScreenState extends State<SecondScreen> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('SecondScreen'),
),
body: Text('update here from first screen'),
);
}
}
CodePudding user response:
Use Getx Storage
:
dependencies:
get_storage: ^2.0.3
Example: In 1st Screen You Need to Store the value :
Initialize storage driver with await:
# main() async { await
GetStorage.init(); runApp(App()); }
use GetStorage through an instance or use directly GetStorage().read('key')
final box = GetStorage();
To read values you use read:
print(box.read('quote'));
// out: GetX is the best
In the 2nd Page you can remove the key:
To remove a key, you can use remove:
box.remove('quote');