-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
223880
committed
Jul 29, 2024
1 parent
fa3c58b
commit 9f00bc0
Showing
3 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'dart:convert'; | ||
|
||
void main() { | ||
runApp(MyApp()); | ||
} | ||
|
||
class MyApp extends StatelessWidget { | ||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp( | ||
home: Scaffold( | ||
appBar: AppBar(title: Text('Dart Library in Flutter')), | ||
body: Center( | ||
child: FutureBuilder<String>( | ||
future: fetchData(), | ||
builder: (context, snapshot) { | ||
if (snapshot.connectionState == ConnectionState.waiting) { | ||
return CircularProgressIndicator(); | ||
} else if (snapshot.hasError) { | ||
return Text('Error: ${snapshot.error}'); | ||
} else { | ||
return Text('Data: ${snapshot.data}'); | ||
} | ||
}, | ||
), | ||
), | ||
), | ||
); | ||
} | ||
|
||
Future<String> fetchData() async { | ||
final response = await http.get(Uri.parse('https://jsonplaceholder.typicode.com/posts/1')); | ||
if (response.statusCode == 200) { | ||
var jsonResponse = jsonDecode(response.body); | ||
return jsonResponse['title']; | ||
} else { | ||
throw Exception('Failed to load data'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters