json_placeholder_plugin 0.0.1
json_placeholder_plugin: ^0.0.1 copied to clipboard
Plugin Flutter para consumir la API pública JSONPlaceholder.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:json_placeholder_plugin/json_placeholder_plugin.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(home: PluginExampleScreen());
}
}
class PluginExampleScreen extends StatefulWidget {
const PluginExampleScreen({super.key});
@override
State<PluginExampleScreen> createState() => _PluginExampleScreenState();
}
class _PluginExampleScreenState extends State<PluginExampleScreen> {
List<Map<String, dynamic>> posts = [];
bool loading = false;
String error = '';
Future<void> fetchPosts() async {
setState(() {
loading = true;
error = '';
});
try {
final data = await JsonPlaceholderPlugin.getPosts();
setState(() => posts = data);
} catch (e) {
setState(() => error = e.toString());
} finally {
setState(() => loading = false);
}
}
@override
void initState() {
super.initState();
fetchPosts();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Plugin JSONPlaceholder')),
body: loading
? const Center(child: CircularProgressIndicator())
: error.isNotEmpty
? Center(child: Text('Error: $error'))
: ListView.builder(
itemCount: posts.length,
itemBuilder: (context, index) {
final post = posts[index];
return ListTile(
title: Text(post['title']),
subtitle: Text(post['body']),
);
},
),
);
}
}