text stringlengths 1 372 |
|---|
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> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.