text
stringlengths
1
474
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController _controller = TextEditingController();
final _channel = WebSocketChannel.connect(
Uri.parse('wss://echo.websocket.events'),
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Form(
child: TextFormField(
controller: _controller,
decoration: const InputDecoration(labelText: 'Send a message'),
),
),
const SizedBox(height: 24),
StreamBuilder(
stream: _channel.stream,
builder: (context, snapshot) {
return Text(snapshot.hasData ? '${snapshot.data}' : '');
},
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _sendMessage,
tooltip: 'Send message',
child: const Icon(Icons.send),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
void _sendMessage() {
if (_controller.text.isNotEmpty) {
_channel.sink.add(_controller.text);
}
}
@override
void dispose() {
_channel.sink.close();
_controller.dispose();
super.dispose();
}
}<code_end>
<topic_end>
<topic_start>Serialization
<topic_end>
<topic_start>
Topics
<topic_end>
<topic_start>JSON and serialization
It is hard to think of a mobile app that doesn’t need to communicate with a
web server or easily store structured data at some point. When making
network-connected apps, the chances are that it needs to consume some good old
JSON, sooner or later.This guide looks into ways of using JSON with Flutter.
It covers which JSON solution to use in different scenarios, and why.info
Terminology: Encoding and serialization are the same
thing—turning a data structure into a string.
Decoding and deserialization are the
opposite process—turning a string into a data structure.
However, serialization also commonly refers to the entire process of
translating data structures to and from a more easily readable format.To avoid confusion, this doc uses “serialization” when referring to the
overall process, and “encoding” and “decoding” when specifically
referring to those processes.<topic_end>
<topic_start>
Which JSON serialization method is right for me?
This article covers two general strategies for working with JSON:Different projects come with different complexities and use cases.
For smaller proof-of-concept projects or quick prototypes,
using code generators might be overkill.
For apps with several JSON models with more complexity,
encoding by hand can quickly become tedious, repetitive,
and lend itself to many small errors.<topic_end>
<topic_start>
Use manual serialization for smaller projects
Manual JSON decoding refers to using the built-in JSON decoder in
dart:convert. It involves passing the raw JSON string to the jsonDecode()
function, and then looking up the values you need in the resulting
Map<String, dynamic>.
It has no external dependencies or particular setup process,
and it’s good for a quick proof of concept.Manual decoding does not perform well when your project becomes bigger.
Writing decoding logic by hand can become hard to manage and error-prone.
If you have a typo when accessing a nonexistent JSON
field, your code throws an error during runtime.If you do not have many JSON models in your project and are
looking to test a concept quickly,
manual serialization might be the way you want to start.
For an example of manual encoding, see
Serializing JSON manually using dart:convert.lightbulb Tip
For hands-on practice deserializing JSON and
taking advantage of Dart 3’s new features,
check out the Dive into Dart’s patterns and records codelab.<topic_end>
<topic_start>
Use code generation for medium to large projects
JSON serialization with code generation means having an external library