-
Notifications
You must be signed in to change notification settings - Fork 103
Syncronize data from the web
Hüseyin Tokpınar edited this page May 2, 2020
·
1 revision
Here we have a JSON data accessible from the web: Todos.json
and our model is here:
const tableTodo = SqfEntityTable(
tableName: 'todos',
primaryKeyName: 'id',
useSoftDeleting: false,
primaryKeyType: PrimaryKeyType.integer_unique,
defaultJsonUrl 'https://jsonplaceholder.typicode.com/todos',
// optional: to synchronize your table with json data from webUrl
fields: [
SqfEntityField('userId', DbType.integer),
SqfEntityField('title', DbType.text),
SqfEntityField('completed', DbType.bool, defaultValue: false)
]);
EXAMPLE 8.2: Fill List from the web with Url (JSON data) and save all
Step 1: Todo.fromWebUrl("URL",(todosList){}) method gets json data from the web and loads into the todosList
Step 2: Call Todo().upsertAll(todosList) method saves all data in your local database
todosList = await Todo.fromWebUrl("https://jsonplaceholder.typicode.com/todos");
final results = await Todo().upsertAll(todosList);
// print upsert Results
for (var res in results) {
print(res.toString());
}
todosList = await Todo().select().top(10).toList();
print(todosList.length.toString() + " matches found\n");
for (var todo in todosList) {
print(todo.toMap());
}
and here are the results:
flutter EXAMPLE 8.1: Fill List from web (JSON data) and upsertAll
flutter -> Todo.fromWeb((todosList) {}
flutter 10 matches found
flutter {id: 1, userId: 1, title: delectus aut autem, completed: false}
flutter {id: 2, userId: 1, title: quis ut nam facilis et officia qui, completed: false}
flutter {id: 3, userId: 1, title: fugiat veniam minus, completed: false}
flutter {id: 4, userId: 1, title: et porro tempora, completed: false}
flutter {id: 5, userId: 1, title: laboriosam mollitia et enim quasi adipisci quia provident illum, completed: false}
flutter {id: 6, userId: 1, title: qui ullam ratione quibusdam voluptatem quia omnis, completed: false}
flutter {id: 7, userId: 1, title: illo expedita consequatur quia in, completed: false}
flutter {id: 8, userId: 1, title: quo adipisci enim quam ut ab, completed: false}
flutter {id: 9, userId: 1, title: molestiae perspiciatis ipsa, completed: false}
flutter {id: 10, userId: 1, title: illo est ratione doloremque quia maiores aut, completed: false}