在Flutter中更新数据 [英] Updating data in Flutter
问题描述
我刚刚开始学习Dart和Flutter,首先,我想开发一个充当服务器的应用程序(我们从 telnet
向其发送消息)。
I just started learning Dart and Flutter and for starting, I would like to develop an application that acts as a server (to which we send messages from telnet
).
因此,目前我有以下两个类:
So at the moment, I have the two following classes:
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => new _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
List<String> _messages = <String>[];
...
}
该应用程序将作为服务器运行。每当服务器收到消息时,我都希望更新列表 _messages
。
So as I said, the app will run as a server. I want to update the list _messages
every time the server receives a message.
我想从更新它另一个类,我们称其为 Server
,我从中调用 HomeScreen.addMessage(String message)
,想要将 _HomeScreenState
设为私有。
I would like to update it from another class, let's call it Server
, from which I call i.e. HomeScreen.addMessage(String message)
and I would also like to keep _HomeScreenState
private.
我已经花了很多时间寻找解决方案,但没有找到
I've searched a lot of time for a solution but didn't find anything suitable for my needs.
你们能帮我吗?
非常感谢!
推荐答案
您可以让您的 State
订阅 Stream
消息。
You can have your State
subscribe to a Stream
of messages.
import 'dart:async';
import 'package:flutter/material.dart';
class Server {
StreamController<String> _controller = new StreamController.broadcast();
void simulateMessage(String message) {
_controller.add(message);
}
Stream get messages => _controller.stream;
}
final server = new Server();
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => new _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
List<String> _messages = <String>[];
StreamSubscription<String> _subscription;
@override
void initState() {
_subscription = server.messages.listen((String message) {
setState(() {
_messages.add(message);
});
});
super.initState();
}
@override
void dispose() {
_subscription.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
TextStyle textStyle = Theme.of(context).textTheme.display2;
return new Scaffold(
appBar: new AppBar(
title: new Text('Telnet Example'),
),
body: new ListView(
children: _messages.map((String message) {
return new Card(
child: new Container(
height: 100.0,
child: new Center(
child: new Text(message, style: textStyle),
),
),
);
}).toList(),
),
floatingActionButton: new FloatingActionButton(
child: new Icon(Icons.add),
onPressed: () {
// simulate a message arriving
server.simulateMessage('Hello Dayrona!');
},
),
);
}
}
class TelnetSample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
theme: new ThemeData.dark(),
home: new HomeScreen(),
);
}
}
void main() {
runApp(new TelnetSample());
}
注意:您可以拥有列表$如果您想使消息c $ c>持久存在,即使用户访问了其他屏幕,也应将它归
Server
类所有。您仍然需要 Stream
或其他通知回调,以使您的 State
知道列表已更改。
Note: You can have the List
of messages be owned by the Server
class if you want to make it persist even if the user visits other screens. You'll still need a Stream
or other notification callback to let your the State
know the list has been changed.
这篇关于在Flutter中更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!