From f3165c2eec93d4c2c3bc1ffac7b06b50e563e52c Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Thu, 17 May 2018 17:18:11 +0200 Subject: [PATCH] Load the notes from the server --- lib/main.dart | 73 +++++++++++++++++++++++++++++++++++++-------------- pubspec.lock | 9 ++++++- pubspec.yaml | 3 +++ 3 files changed, 65 insertions(+), 20 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index d12a2b14..7eade7e2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,33 +1,52 @@ -import 'package:flutter/material.dart'; +import 'dart:async'; +import 'dart:convert'; -class Journal { - final DateTime creationDateTime; +import 'package:flutter/material.dart'; +import 'package:http/http.dart' as http; + +class Note { + final DateTime createdAt; final String body; - const Journal({this.creationDateTime, this.body}); + const Note({this.createdAt, this.body}); - factory Journal.fromJson(Map json) { - return new Journal( - creationDateTime: json['creationDateTime'], - body: json['body'], + factory Note.fromJson(Map json) { + print(json) + return new Note( + createdAt: DateTime.parse(json['createdAt']), + body: json['data'], ); } } +Future> fetchNotes() async { + final response = await http.get('http://192.168.1.132:8000/notes'); + final responseJson = json.decode(response.body); + + var notes = []; + for (var postJson in responseJson) { + notes.add(new Note.fromJson(postJson)); + } + + return notes; +} + // How to load this dynamically? // I can put this in a widget with a state // and do an async call. -var state = [ - Journal( - creationDateTime: new DateTime.now(), +/* +var state = [ + Note( + createdAt: new DateTime.now(), body: "The quick brown fox jumped over the very lazy dog, who then ran" "all around the garden untill he fell down", ), - Journal( - creationDateTime: new DateTime.now().subtract(new Duration(days: 1)), + Note( + createdAt: new DateTime.now().subtract(new Duration(days: 1)), body: "This is the body", ), ]; +*/ void main() => runApp(new MyApp()); @@ -45,15 +64,31 @@ class JournalList extends StatelessWidget { appBar: new AppBar( title: new Text('Journal'), ), - body: _buildSuggestions(context), floatingActionButton: createButton, + body: new FutureBuilder>( + future: fetchNotes(), + builder: (context, snapshot) { + if (snapshot.hasData) { + var notes = snapshot.data; + return _buildSuggestions(context, notes); + } else if (snapshot.hasError) { + return new Text("${snapshot.error}"); + } + + return new CircularProgressIndicator(); + }), ); } - Widget _buildRow(BuildContext context, Journal journal) { - var title = journal.creationDateTime.toString(); + Widget _buildRow(BuildContext context, Note journal) { + var title = journal.createdAt.toString(); var time = "10:24"; + var body = journal.body; + if (body.length >= 100) { + body = body.substring(0, 100); + } + body = body.replaceAll("\n", " "); return new ListTile( isThreeLine: true, @@ -92,15 +127,15 @@ So now what is going to happen? Navigator.of(context).push(route); } - Widget _buildSuggestions(BuildContext context) { + Widget _buildSuggestions(BuildContext context, List notes) { return new ListView.builder( padding: const EdgeInsets.all(8.0), itemBuilder: (context, i) { - if (i >= state.length) { + if (i >= notes.length) { return null; } //if (i.isOdd) return new Divider(); - return _buildRow(context, state[i]); + return _buildRow(context, notes[i]); }, ); } diff --git a/pubspec.lock b/pubspec.lock index b01e05be..5e65f110 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -110,7 +110,7 @@ packages: source: hosted version: "0.13.3" http: - dependency: transitive + dependency: "direct main" description: name: http url: "https://pub.dartlang.org" @@ -130,6 +130,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.1.1" + intl: + dependency: "direct main" + description: + name: intl + url: "https://pub.dartlang.org" + source: hosted + version: "0.15.6" io: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index d7935d33..b79e0352 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,6 +4,9 @@ description: A simple journaling app. dependencies: flutter: sdk: flutter + http: "^0.11.3+16" + intl: "^0.15.6" + dev_dependencies: flutter_test: