How to fetch data snapshot from Firebase and put that in a separate dart file in the same project?

Viewed 75

My group was initially asked to create a prototype for a Flutter application called 'Smart Gloves'. Now later on, we have been assigned to use Firebase as our backend server, fetch data from it and display it like we did in the app prototype. However, I am unable to successfully fetch anything from the database.

Prototype 1 reference given at the bottom

this is the code from prototype

edu_module.dart

const List<EduModule> eduModules = [
  EduModule(
    title: 'Interrupted Stitch',
    subtitle: 'subt lorem ipsum',
    learnModule: LearnModule(
        video: null,
        graph:
            'https://google.github.io/charts/flutter/example/scatter_plot_charts/shapes_full.png',
        techniques: [
          'Some key technique here.',
          'Another key technique here.',
          'Final consideration here.'
        ]),
    practiceModule: PracticeModule(
        instructions: 'inst lorem ipsum',
        video: null,
        graph:
            'https://google.github.io/charts/flutter/example/line_charts/segments_full.png',
        dummyResult: PracticeResult()),
    challenge: ChallengeModule(),
  )

edu_module_screen.dart

class EduModuleScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Education Library'),
      ),
     body: SafeArea(
        child: ListView.builder(
          itemCount: eduModules.length,
          itemBuilder: (BuildContext context, int index) =>
              EduModuleTile(eduModules[index]),
        ),
      ),
    );
  }
}

this is my current code in edu_module.dart

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart'; // Importing cloud firestore package to link this with Firebase


class eduModule extends StatefulWidget {
  FirebaseFirestore firestore = FirebaseFirestore.instance;

  @override
  _eduModuleState createState() => _eduModuleState();
}

class _eduModuleState extends State<eduModule> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: Text('Education Library'),
          ),
          body: Center(
            child: StreamBuilder(
                stream: FirebaseFirestore.instance
                    .collection('Education')
                    .snapshots(),
                builder: (context, AsyncSnapshot snapshot) {
                  if (!snapshot.hasData) {
                    return Text('Error 404! Not Found');
                  }
                  return ListView.builder(
                    itemCount: snapshot.data.docs.length,
                    itemBuilder: (context, index) {
                      DocumentSnapshot result = snapshot.data.docs[index];
                      return Card(
                        child: ListTile(
                            title: Text(result['name'].toString())
                        ),
                      );
                    },
                  );
                }
            ),
          ),
        )
    );
  }
}

Prototype Firebase

0 Answers
Related