lissof

FirebaseAnimatedList problem

Apr 18th, 2023
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.67 KB | Source Code | 0 0
  1. import 'package:firebase_auth/firebase_auth.dart';
  2. import 'package:firebase_core/firebase_core.dart';
  3. import 'package:firebase_database/firebase_database.dart';
  4. import 'package:firebase_database/ui/firebase_animated_list.dart';
  5. import 'package:flutter/material.dart';
  6. import 'firebase_options.dart';
  7.  
  8. void main() {
  9.   runApp( MyApp());
  10. }
  11.  
  12. class MyApp extends StatelessWidget {
  13.   const MyApp({Key? key}) : super(key: key);
  14.  
  15.   Future<UserCredential> _signIn() async {
  16.     print('init app');
  17.     return Firebase.initializeApp(
  18.       options: DefaultFirebaseOptions.currentPlatform,
  19.     ).then((value) async {
  20.       print('signing in');
  21.       return await FirebaseAuth.instance.signInWithEmailAndPassword(
  22.         email: "[email protected]",
  23.         password: "mypassword",
  24.       );
  25.     });
  26.   }
  27.  
  28.   @override
  29.   Widget build(BuildContext context) {
  30.     return MaterialApp(
  31.       debugShowCheckedModeBanner: false,
  32.       home: FutureBuilder<UserCredential>(
  33.         future: _signIn(),
  34.         builder: (context, snapshot) {
  35.           if (snapshot.connectionState == ConnectionState.waiting) {
  36.             return const Scaffold(
  37.               body: Center(
  38.                 child: CircularProgressIndicator(),
  39.               ),
  40.             );
  41.           } else if (snapshot.hasError) {
  42.             return Scaffold(
  43.               body: Center(
  44.                 child: Text("Error signing in: ${snapshot.error}"),
  45.               ),
  46.             );
  47.           } else {
  48.             return const StateHome();
  49.           }
  50.         },
  51.       ),
  52.     );
  53.   }
  54. }
  55.  
  56. class StateHome extends StatefulWidget {
  57.   const StateHome({Key? key}) : super(key: key);
  58.  
  59.   @override
  60.   State<StateHome> createState() => _StateHomeState();
  61. }
  62.  
  63. class _StateHomeState extends State<StateHome> {
  64.   @override
  65.   Widget build(BuildContext context) {
  66.     return Scaffold(
  67.         appBar: AppBar(
  68.           title: const Text("Home"),
  69.           actions: [
  70.             IconButton(
  71.               icon: const Icon(Icons.logout),
  72.               onPressed: () async {
  73.                 FirebaseAuth.instance.signOut().then((value) => print('logged out'));
  74.               },
  75.             )
  76.           ],
  77.         ),
  78.         body: Column(children: [
  79.             Expanded(
  80.               child: FirebaseAnimatedList(
  81.                   query: FirebaseDatabase.instance.ref().child('msg'),
  82.                   itemBuilder: (BuildContext context, DataSnapshot snapshot,
  83.                       Animation<double> animation, int index) {
  84.                       return ListTile(
  85.                         title: Text("message.content"),
  86.                       );
  87.                   }),
  88.             ),
  89.  
  90.         ]
  91.         )
  92.     );
  93.   }
  94. }
  95.  
  96.  
Advertisement
Add Comment
Please, Sign In to add comment