FirebaseAuthでのログイン機能実装した時のビルドエラー
ログイン画面のコード(signup.dart)
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'login_page.dart';
import 'signup_model.dart';
class SignupPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final mailController = TextEditingController();
final passwordController = TextEditingController();
return ChangeNotifierProvider<SignUpModel>(
create: (_) => SignUpModel(),
child: Scaffold(
backgroundColor: Colors.white,
body: Consumer<SignUpModel>(
builder: (context, model, child) {
return SafeArea(
child: Container(
padding: EdgeInsets.only(left: 16, right: 16),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
height: 50,
),
Text(
"Create Account,",
style: TextStyle(
fontSize: 26, fontWeight: FontWeight.bold),
),
SizedBox(
height: 6,
),
Text(
"Sign up to get started!",
style: TextStyle(
fontSize: 20, color: Colors.grey.shade400),
),
],
),
Column(
children: <Widget>[
TextField(
controller: mailController,
obscureText: true,
onChanged: (text) {
model.mail = text;
},
decoration: InputDecoration(
labelText: "Email ID",
labelStyle: TextStyle(
fontSize: 14,
color: Colors.grey.shade400,
fontWeight: FontWeight.w600),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide:
BorderSide(color: Colors.grey.shade300),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
floatingLabelBehavior: FloatingLabelBehavior.auto,
),
),
SizedBox(
height: 16,
),
TextField(
controller: passwordController,
obscureText: true,
onChanged: (text) {
model.password = text;
},
decoration: InputDecoration(
labelText: "Password",
labelStyle: TextStyle(
fontSize: 14,
color: Colors.grey.shade400,
fontWeight: FontWeight.w600),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide:
BorderSide(color: Colors.grey.shade300),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
floatingLabelBehavior: FloatingLabelBehavior.auto,
),
),
SizedBox(
height: 30,
),
Container(
height: 50,
child: FlatButton(
color: Colors.yellow[700],
onPressed: () async {
try {
await model.signUp();
} catch (e) {
}
},
padding: EdgeInsets.all(0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
child: Ink(
decoration: BoxDecoration(),
child: Container(
alignment: Alignment.center,
constraints: BoxConstraints(
minHeight: 50, maxWidth: double.infinity),
child: Text(
"Sign up",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white),
textAlign: TextAlign.center,
),
),
),
),
),
SizedBox(
height: 16,
),
SizedBox(
height: 30,
),
],
),
Padding(
padding: const EdgeInsets.only(bottom: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"すでにアカウントをお持ちですか?",
style: TextStyle(fontWeight: FontWeight.bold),
),
GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => LoginPage(),
),
);
},
child: Text(
"login",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Color(0xFF3b5999),
),
),
)
],
),
)
],
),
),
);
},
),
),
);
}
}
モデルページ(signup_model.dart)
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class SignUpModel extends ChangeNotifier {
String mail = '';
String password = '';
final FirebaseAuth _auth = FirebaseAuth.instance;
Future signUp() async {
if (mail.isEmpty) {
throw ('メールアドレスを入力してください');
}
if (password.isEmpty) {
throw ('パスワードを入力してください');
}
// todo
final User user = (await _auth.createUserWithEmailAndPassword(
email: mail,
password: password,
))
.user!;
final email = user.email;
FirebaseFirestore.instance.collection('users').add(
{
'email': email,
'createdAt': Timestamp.now(),
},
);
}
}