django_rest_framework_client 0.0.6
django_rest_framework_client: ^0.0.6 copied to clipboard
A complete implementation of Authentication and Rest API connection with the backends that use Django Rest Framework. Supports refreshing and storing tokens, supports SimpleJWT
import 'package:flutter/material.dart';
import 'package:django_rest_framework_client/django_rest_framework_client.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'DRF Client',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.green),
useMaterial3: true,
),
home: const MyHomePage(title: 'DRF Client'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final DRFApi api = DRFApi('https://smartlib.tijorat.org/api/v1', usernameField: 'mobile');
String responseText = '';
TextEditingController usernameController = TextEditingController();
TextEditingController passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: const Text("DRF Client"),
),
body: ListView(
children: [
ElevatedButton(
onPressed: () => _makeGetRequest(),
child: const Text('GET Request'),
),
const SizedBox(height: 20),
Text(responseText),
],
),
floatingActionButton: FloatingActionButton(
onPressed: ()async{
String res = await api.login("998", "");
debugPrint(res);
if(context.mounted){
_showSnackBar(context, res);
}
},
child: const Icon(Icons.add),
),
);
}
Future<void> _makeGetRequest() async {
try {
final response = await api.get('books/books');
setState(() {
responseText = response.body;
});
} catch (e) {
setState(() {
responseText = 'Error: ${e.toString()}';
});
}
}
void _showSnackBar(BuildContext context, String message) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message)));
}
}