#pub (cloud_firestore) 取得しました。
下記のコードの Firestore と DocumentSnapshot がエラーで実行できません
build.gradle など裏の設定についてご教授お願いします
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
void main() => runApp(MyHomePage());
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var count = 1;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Firestore Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Firestore Demo'),
),
body: createListView(),
floatingActionButton: FloatingActionButton(
onPressed: () {
Firestore.instance.collection('books').add({
'title': 'タイトル$count',
'author': '著者$count',
});
count += 1;
},
child: Icon(Icons.add),
),
),
);
}
createListView() {
Firestore.instance.collection('books').snapshots().listen((data) {
print(data);
});
return StreamBuilder( stream: Firestore.instance.collection('books').snapshots(), builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) { // エラーの場合 if (snapshot.hasError) { return Text('Error: ${snapshot.error}'); } // 通信中の場合 switch (snapshot.connectionState) { case ConnectionState.waiting: return Text('Loading ...'); default: return ListView( children: snapshot.data.documents.map((DocumentSnapshot document) { return new ListTile( title: new Text(document['title']), subtitle: new Text(document['author']), ); }).toList(), ); } }, );
}
}