text stringlengths 1 372 |
|---|
final string title; |
const album({required this.id, required this.title}); |
factory Album.fromJson(Map<String, dynamic> json) { |
return switch (json) { |
{ |
'id': int id, |
'title': string title, |
} => |
album( |
id: id, |
title: title, |
), |
_ => throw const FormatException('Failed to load album.'), |
}; |
} |
} |
void main() { |
runApp(const MyApp()); |
} |
class MyApp extends StatefulWidget { |
const MyApp({super.key}); |
@override |
State<MyApp> createState() { |
return _MyAppState(); |
} |
} |
class _MyAppState extends State<MyApp> { |
late Future<Album> _futureAlbum; |
@override |
void initState() { |
super.initState(); |
_futureAlbum = fetchAlbum(); |
} |
@override |
widget build(BuildContext context) { |
return MaterialApp( |
title: 'delete data example', |
theme: ThemeData( |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), |
), |
home: scaffold( |
appBar: AppBar( |
title: const Text('Delete data example'), |
), |
body: center( |
child: FutureBuilder<Album>( |
future: _futureAlbum, |
builder: (context, snapshot) { |
// if the connection is done, |
// check for response data or an error. |
if (snapshot.connectionstate == ConnectionState.done) { |
if (snapshot.hasdata) { |
return column( |
mainAxisAlignment: MainAxisAlignment.center, |
children: <widget>[ |
text(snapshot.data?.title ?? 'deleted'), |
ElevatedButton( |
child: const Text('Delete data'), |
onPressed: () { |
setState(() { |
_futureAlbum = |
deleteAlbum(snapshot.data!.id.toString()); |
}); |
}, |
), |
], |
); |
} else if (snapshot.haserror) { |
return text('${snapshot.error}'); |
} |
} |
// by default, show a loading spinner. |
return const CircularProgressIndicator(); |
}, |
), |
), |
), |
); |
} |
} |
<code_end> |
<topic_end> |
<topic_start> |
communicate with WebSockets |
in addition to normal HTTP requests, |
you can connect to servers using WebSockets. |
WebSockets allow for two-way communication with a server |
without polling. |
in this example, connect to a |
test WebSocket server sponsored by lob.com. |
the server sends back the same message you send to it. |
this recipe uses the following steps: |
<topic_end> |
<topic_start> |
1. connect to a WebSocket server |
the web_socket_channel package provides the |
tools you need to connect to a WebSocket server. |
the package provides a WebSocketChannel |
that allows you to both listen for messages |
from the server and push messages to the server. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.