每当我运行我的 Flutter 应用程序时,我都会收到一个空错误

时间:2021-04-20 15:14:51

标签: flutter

这是我收到错误的主页代码

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:localeventsapp/Screens/Login/login_screen.dart';
import 'package:localeventsapp/model/category.dart';
import 'package:localeventsapp/model/event.dart';
import 'package:localeventsapp/styleguide.dart';
import 'package:localeventsapp/ui/event_details/event_details_page.dart';
import 'package:localeventsapp/ui/homepage/form_widget.dart';
import 'package:provider/provider.dart';
import '../../app_state.dart';
import '../../authentication_service.dart';
import 'category_widget.dart';
import 'event_widget.dart';
import 'home_page_background.dart';

CollectionReference users = FirebaseFirestore.instance.collection("Users");

FirebaseAuth auth =  FirebaseAuth.instance;
String uid = auth.currentUser.uid.toString();


class MainPage extends StatefulWidget{
  HomePage createState()=> HomePage();
}

class HomePage extends State<MainPage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ChangeNotifierProvider<AppState>(
        create: (_) => AppState(),
        child: Stack(
          children: <Widget>[
            HomePageBackground(
              screenHeight: MediaQuery.of(context).size.height,
            ),
            SafeArea(
              child: SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 32.0),
                      child: Row(
                        children: <Widget>[
                          Text(
                            "TuLink",
                            style: fadedTextStyle,
                          ),
                          Spacer(),
                        ],
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 32.0),
                      child: Text(
                        "Welcome!",
                        style: whiteHeadingTextStyle,
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 24.0),
                      child: Consumer<AppState>(
                        builder: (context, appState, _) =>
                          SingleChildScrollView(
                          scrollDirection: Axis.horizontal,
                          child: Row(
                            children: <Widget>[
                              for (final category in categories)
                                CategoryWidget(category: category),

                            ],
                          ),
                        ),
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 16.0),
                      child: Consumer<AppState>(
                        builder: (context, appState, _) => Column(
                          children: <Widget>[
                            for (final event in userEvents.where((e) => e
                                .categoryIds
                                .contains(appState.selectedCategoryId)))
                              GestureDetector(
                                onTap: () {
                                  Navigator.of(context).push(
                                    MaterialPageRoute(
                                      builder: (context) =>
                                          EventDetailsPage(event: event),
                                    ),
                                  );
                                },
                                child: EventWidget(
                                  event: event,
                                ),
                              )
                          ],
                        ),
                      ),
                    ),
                    FloatingActionButton.extended(
                     onPressed: () {
                       Navigator.push(context,
                       MaterialPageRoute(builder: (context) => FormPage()));
                     },
                     label: Text('Create'),
                     icon: Icon(Icons.create),
                     elevation: 2,
                     shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0))),
                     backgroundColor: Color(0xFF6F35A5),
                    ),
                    ElevatedButton(
                  child: Text('Sign Out',
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: 16,
                      )),
                  onPressed: () {
                    context.read<AuthenticationService>().signOut();
                    Navigator.of(context).push(MaterialPageRoute(builder: (context) => LoginScreen()));
                  }),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }


}

class CircularButton extends StatelessWidget {
  final double width;
  final double height;
  final Color color;
  final Icon icon;
  final Function onClick;

  CircularButton(
      {this.color, this.width, this.height, this.icon, this.onClick});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(color: color, shape: BoxShape.circle),
      width: width,
      height: height,
      child: IconButton(
        icon: icon,
        enableFeedback: true,
        onPressed: onClick,
      ),
    );
  }
}

特别是这部分:

                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 16.0),
                      child: Consumer<AppState>(
                        builder: (context, appState, _) => Column(
                          children: <Widget>[
                            for (final event in userEvents.where((e) => e
                                .categoryIds
                                .contains(appState.selectedCategoryId)))
                              GestureDetector(
                                onTap: () {
                                  Navigator.of(context).push(
                                    MaterialPageRoute(
                                      builder: (context) =>
                                          EventDetailsPage(event: event),
                                    ),
                                  );
                                },
                                child: EventWidget(
                                  event: event,
                                ),
                              )
                          ],
                        ),
                      ),
                    ),

它说在 null 上调用了方法“where”。 The errror

这是 userEvents 所在的类:

import 'package:cloud_firestore/cloud_firestore.dart';

CollectionReference events = FirebaseFirestore.instance.collection("Events");
DocumentReference eventDocs = events.doc();
List<dynamic> userEventsList;
// List<Event> firebaseEvents = <Event>[];

class Event {
  final String eventName, eventID, location, duration, description, creatorID, imagePath;
  String category;
  final List categoryID, galleryImages;

  Event(
        {
          this.imagePath,
          this.eventName,
          this.eventID,
          this.location,
          this.duration,
          this.description,
          this.creatorID,
          this.categoryID,
          this.galleryImages,
        }
      );
}
Future<void> getData() async {
  final eventsList = [];

  Stream<QuerySnapshot> eventDocs = events.snapshots();

  eventDocs.forEach((element) {
    element.docs.asMap().forEach((key, value) {

      final firebaseEventDocs = Event(
          imagePath: "assets/event_images/5_km_downtown_run.jpeg",
          eventName: element.docs[key]["eventName"],
          description: element.docs[key]["description"],
          location: element.docs[key]["location"],
          duration: element.docs[key]["duration"],
          galleryImages: [],
          categoryID: [0,element.docs[key]["category"]]);

      eventsList.add({
        firebaseEventDocs,
      });
    });
  });
  userEventsList = eventsList;
}

final userEvents = userEventsList;

我的想法是从 Firestore 中存储的文档中检索一些信息,并使用它们来填充一些将显示在主页上的卡片,但我很困惑。 关于错误可能在哪里的任何想法?

0 个答案:

没有答案