mutation support added

multiple data, error/mutate listeners on query
This commit is contained in:
Kingkor Roy Tirtho
2022-06-19 16:53:20 +06:00
parent 617d920464
commit 7d337e02da
10 changed files with 532 additions and 19 deletions
+12
View File
@@ -2,6 +2,7 @@ import 'dart:math';
import 'package:example/another_component.dart';
import 'package:example/lazy_query.dart';
import 'package:example/mutation_example.dart';
import 'package:example/query_with_external_data.dart';
import 'package:fl_query/fl_query.dart';
import 'package:flutter/material.dart';
@@ -137,6 +138,17 @@ class _MyHomePageState extends State<MyHomePage> {
);
},
),
const SizedBox(height: 10),
ElevatedButton(
child: const Text("Mutation Example"),
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const MutationExample(),
),
);
},
),
const AnotherComponent(),
],
),
@@ -0,0 +1,94 @@
import 'dart:convert';
import 'dart:math';
import 'package:fl_query/models/mutation_job.dart';
import 'package:fl_query/mutation_builder.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
final postSomethingJob = MutationJob<Map, Map<String, dynamic>>(
mutationKey: "post-something-job",
task: (key, data) async {
final response = await http.post(
Uri.parse(
"https://jsonplaceholder.typicode.com/posts",
),
headers: {'Content-type': 'application/json; charset=UTF-8'},
body: jsonEncode(data),
);
return jsonDecode(response.body);
},
);
class MutationExample extends StatefulWidget {
const MutationExample({Key? key}) : super(key: key);
@override
State<MutationExample> createState() => _MutationExampleState();
}
class _MutationExampleState extends State<MutationExample> {
late TextEditingController titleController;
late TextEditingController bodyController;
late int id;
@override
void initState() {
super.initState();
id = Random().nextInt(2000000);
titleController = TextEditingController();
bodyController = TextEditingController();
}
@override
void dispose() {
titleController.dispose();
bodyController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Post Something")),
body: MutationBuilder<Map, Map<String, dynamic>>(
job: postSomethingJob,
builder: (context, mutation) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: [
TextField(
controller: titleController,
decoration: const InputDecoration(labelText: "Title"),
),
TextField(
controller: bodyController,
decoration: const InputDecoration(labelText: "Body"),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
final title = titleController.value.text;
final body = bodyController.value.text;
if (body.isEmpty || title.isEmpty) return;
mutation.mutate({
"title": title,
"body": body,
"id": id,
}, onData: (data) {
// resetting the form
titleController.text = "";
bodyController.text = "";
});
},
child: const Text("Post"),
),
const SizedBox(height: 20),
if (mutation.hasData) Text("Response\n${mutation.data}")
],
),
);
}),
);
}
}
+21
View File
@@ -81,6 +81,20 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.4"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.1"
lints:
dependency: transitive
description:
@@ -163,6 +177,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.9"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
vector_math:
dependency: transitive
description:
+1
View File
@@ -35,6 +35,7 @@ dependencies:
cupertino_icons: ^1.0.2
fl_query:
path: ../fl_query
http: ^0.13.4
dev_dependencies:
flutter_test: