A new Flutter project.
This project is a starting point for a Flutter application.
A few resources to get you started if this is your first Flutter project:
For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.
----Podfile---------------------- platform :ios, '12.0' がコメントアウトされていた場合は直す. target 'Runner' do use_frameworks! use_modular_headers! pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '8.6.0' flutter_install_all_ios_pods File.dirname(File.realpath(FILE)) end
firestoreを使う場合は上記を設定しておかないとpodinstallにとんでもない時間がかかる.
----------firebase/sample--------------
import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart';
void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp();
runApp(MyApp()); }
class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( // This is the theme of your application.
primarySwatch: Colors.blue,
),
home: BookListPage(),
);
} }
class BookListPage extends StatelessWidget { final Stream _usersStream = FirebaseFirestore.instance.collection('books').snapshots();
@override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( "本一覧", )), body: StreamBuilder( stream: _usersStream, builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.hasError) { return Text('Something went wrong'); }
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return ListView(
children: snapshot.data!.docs.map((DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return ListTile(
title: Text(data['title']),
subtitle: Text(data['author']),
);
}).toList(),
);
}),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
} }