Skip to content

Commit d3432f4

Browse files
author
Frank Treacy
committed
add example dart app
1 parent 5736d0a commit d3432f4

13 files changed

+979
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
TO-DO.md
2+
/tmp
23

34
# Miscellaneous
45
*.class

example/.vscode/launch.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Dart",
9+
"program": "lib/main.dart",
10+
"request": "launch",
11+
"type": "dart"
12+
}
13+
]
14+
}

example/lib/main.dart

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import 'dart:io';
2+
import 'package:flutter_data/flutter_data.dart';
3+
4+
import 'main.data.dart';
5+
import 'model/comment.dart';
6+
import 'model/post.dart';
7+
import 'model/user.dart';
8+
9+
// NOTE: FOR A COMPLETE FLUTTER EXAMPLE PLEASE SEE
10+
// https://github.com/flutterdata/flutter_data_todos
11+
12+
void main() async {
13+
Directory _dir;
14+
15+
try {
16+
_dir = await Directory('../tmp').create();
17+
final manager = await FlutterData.init(_dir);
18+
final locator = manager.locator;
19+
20+
Repository<User> usersRepo = locator<Repository<User>>();
21+
Repository<Post> postsRepo = locator<Repository<Post>>();
22+
User user;
23+
24+
try {
25+
user = await usersRepo.findOne('1');
26+
} on DataException catch (e) {
27+
if (e.status == HttpStatus.notFound) {
28+
print('not found');
29+
}
30+
}
31+
32+
var user2 = User(id: 102, name: 'new name', email: '[email protected]');
33+
await user2.save();
34+
35+
User(id: 102, name: 'new name', email: '[email protected]');
36+
37+
var p3 = Post(
38+
id: 102,
39+
title: 'new name',
40+
41+
user: user2.asBelongsTo,
42+
comments: [Comment(id: 1, body: 'bla')].asHasMany);
43+
44+
assert(p3.body == '[email protected]');
45+
assert(p3.user.value.email == user2.email);
46+
47+
var post = await postsRepo.findOne('1', params: {'_embed': 'comments'});
48+
49+
print(post.comments.map((c) => c.body));
50+
51+
assert(user.name == post.user.value.name);
52+
53+
var stream = usersRepo.watchAll().stream;
54+
55+
await for (var state in stream) {
56+
print(state.length);
57+
}
58+
} finally {
59+
await _dir.delete(recursive: true);
60+
}
61+
}

example/lib/main.data.dart

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
3+
// GENERATED CODE - DO NOT MODIFY BY HAND
4+
// ignore_for_file: directives_ordering
5+
6+
import 'dart:io';
7+
import 'package:flutter_data/flutter_data.dart';
8+
9+
10+
import 'package:jsonplaceholder_example/model/post.dart';
11+
import 'package:jsonplaceholder_example/model/user.dart';
12+
import 'package:jsonplaceholder_example/model/comment.dart';
13+
14+
extension FlutterData on DataManager {
15+
16+
static Future<DataManager> init(Directory baseDir, {bool autoModelInit = true, bool clear = true, Function(void Function<R>(R)) also}) async {
17+
assert(baseDir != null);
18+
19+
final injection = DataServiceLocator();
20+
21+
final manager = await DataManager(autoModelInit: autoModelInit).init(baseDir, injection.locator, clear: clear);
22+
injection.register(manager);
23+
final postLocalAdapter = await $PostLocalAdapter(manager).init();
24+
injection.register(postLocalAdapter);
25+
injection.register<Repository<Post>>($PostRepository(postLocalAdapter));
26+
27+
final userLocalAdapter = await $UserLocalAdapter(manager).init();
28+
injection.register(userLocalAdapter);
29+
injection.register<Repository<User>>($UserRepository(userLocalAdapter));
30+
31+
final commentLocalAdapter = await $CommentLocalAdapter(manager).init();
32+
injection.register(commentLocalAdapter);
33+
injection.register<Repository<Comment>>($CommentRepository(commentLocalAdapter));
34+
35+
36+
if (also != null) {
37+
// ignore: unnecessary_lambdas
38+
also(<R>(R obj) => injection.register<R>(obj));
39+
}
40+
41+
return manager;
42+
43+
}
44+
45+
46+
47+
}
48+
49+

example/lib/model/adapters.dart

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import 'package:flutter_data/flutter_data.dart';
2+
3+
mixin JSONPlaceholderAdapter<T extends DataSupport<T>>
4+
on StandardJSONAdapter<T> {
5+
@override
6+
String get baseUrl => 'https://jsonplaceholder.typicode.com/';
7+
8+
@override
9+
String get identifierSuffix => 'Id';
10+
}

example/lib/model/comment.dart

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import 'package:meta/meta.dart';
2+
import 'package:flutter_data/flutter_data.dart';
3+
import 'package:json_annotation/json_annotation.dart';
4+
import 'package:flutter_data/annotations.dart';
5+
6+
import 'adapters.dart';
7+
import 'post.dart';
8+
9+
part 'comment.g.dart';
10+
11+
@JsonSerializable()
12+
@DataRepository([StandardJSONAdapter, JSONPlaceholderAdapter])
13+
class Comment extends DataSupport<Comment> {
14+
@override
15+
final int id;
16+
final String body;
17+
bool approved = false;
18+
final BelongsTo<Post> post;
19+
20+
Comment({
21+
this.id,
22+
@required this.body,
23+
this.approved,
24+
BelongsTo<Post> post,
25+
}) : post = post ?? BelongsTo<Post>();
26+
27+
factory Comment.fromJson(Map<String, dynamic> json) =>
28+
_$CommentFromJson(json);
29+
Map<String, dynamic> toJson() => _$CommentToJson(this);
30+
}

example/lib/model/comment.g.dart

+78
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/lib/model/post.dart

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import 'package:meta/meta.dart';
2+
import 'package:flutter_data/flutter_data.dart';
3+
import 'package:json_annotation/json_annotation.dart';
4+
5+
import 'package:flutter_data/annotations.dart';
6+
import 'adapters.dart';
7+
import 'comment.dart';
8+
import 'user.dart';
9+
10+
part 'post.g.dart';
11+
12+
@JsonSerializable()
13+
@DataRepository([StandardJSONAdapter, JSONPlaceholderAdapter])
14+
class Post extends DataSupport<Post> {
15+
@override
16+
final int id;
17+
final String title;
18+
final String body;
19+
final HasMany<Comment> comments;
20+
final BelongsTo<User> user;
21+
22+
Post({
23+
this.id,
24+
@required this.title,
25+
@required this.body,
26+
HasMany<Comment> comments,
27+
BelongsTo<User> user,
28+
}) : comments = comments ?? HasMany<Comment>(),
29+
user = user ?? BelongsTo<User>();
30+
31+
factory Post.fromJson(Map<String, dynamic> json) => _$PostFromJson(json);
32+
Map<String, dynamic> toJson() => _$PostToJson(this);
33+
}

example/lib/model/post.g.dart

+91
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)