Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

undifined string name. How can i solve this error without getting other one's

how to fix this error: undifined name name

in line 64 i have defined the string name but in line 117 the string name is undifined and if i try to make it defined there come’s an other error in line 60: The body might complete normally, causing ‘null’ to be returned, but the return type, ‘Widget’, is a potentially non-nullable type and if i fix this i get an black screen.

so in short can anyone help me with this error without getting other errors

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import 'dart:io';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_svg/svg.dart';
import 'package:hetmaantje/arbo.dart';
import 'package:hetmaantje/inschrijven.dart';
import 'package:hetmaantje/quiz_screen.dart';
import 'package:hetmaantje/utils1/colors.dart';
import 'package:hetmaantje/todolist.dart';
import 'package:page_transition/page_transition.dart';
import 'package:path_provider/path_provider.dart';
import 'contactpage.dart';

class home extends StatefulWidget {
  const home({Key? key}) : super(key: key);

  @override
  State<home> createState() => _homeState();

}

class _homeState extends State<home> {
  String pathPDF = "assets/arbo";

  @override

  void initState() {
    super.initState();
    fromAsset('assets/arbo.pdf', 'arbo.pdf').then((f) {
      setState(() {
        pathPDF = f.path;
      });
    });
  }
  Future<File> fromAsset(String asset, String filename) async {
    Completer<File> completer = Completer();
    try {
      var dir = await getApplicationDocumentsDirectory();
      File file = File("${dir.path}/$filename");
      var data = await rootBundle.load(asset);
      var bytes = data.buffer.asUint8List();
      await file.writeAsBytes(bytes, flush: true);
      completer.complete(file);
    } catch (e) {
      throw Exception('Error parsing asset file!');
    }

    return completer.future;

  }
  final FirebaseAuth auth = FirebaseAuth.instance;
  late final User? user = auth.currentUser;
  late final uid = user?.uid;

  Widget build(BuildContext context) {
    DocumentReference documentReference = FirebaseFirestore.instance.collection(uid!).doc("name");
    documentReference.get().then((datasnapshot){

      String name = datasnapshot.data().toString();
    });
    FirebaseFirestore.instance.collection(uid!);

    // to get size
    var size = MediaQuery.of(context).size;

    // style
    var cardTextStyle = TextStyle(
        fontFamily: "Montserrat Regular",
        fontSize: 14,
        color: Color.fromRGBO(63, 63, 63, 1));


    return Scaffold(

      body: Stack(
        children: <Widget>[
          Container(
            height: size.height * 1,
            decoration: BoxDecoration (gradient: LinearGradient(colors: [
          hexStringToColor("FFBEC8"),
          hexStringToColor("fe98a8"),
          hexStringToColor("FC98A7"),
          hexStringToColor("#FF8799")
        ]
          ))),
          SafeArea(
            child: Padding(
              padding: EdgeInsets.all(16.0),
              child: Column(
                children: <Widget>[
                  Container(
                    
                    height: 64,
                    margin: EdgeInsets.only(bottom: 20),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        CircleAvatar(
                          radius: 29,
                          backgroundImage: AssetImage(
                              'assets/logo.png'),
                        ),
                        SizedBox(
                          width: 16,
                        ),
                        Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Text(
                              
                        "$name",
                              style: TextStyle(
                                  fontFamily: "Montserrat Medium",
                                  color: Colors.white,
                                  fontSize: 20),
                            ),

                          ],
                        )
                      ],
                    ),

                  ),

                  Expanded(
                    child: GridView.count(
                      mainAxisSpacing: 10,
                      crossAxisSpacing: 10,
                      primary: false,
                      crossAxisCount: 2,
                      children: <Widget>[
                        Card(
                          color: Colors.grey.shade100,
                          shape:RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(8)
                          ),

                          elevation: 4,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
    //=> PDFScreen(path: pathPDF)),);
                            children: <Widget>[
                              GestureDetector(
                                onTap:
                          () =>
                        Navigator.of(context).push(PageTransition(
                      child: PDFScreen(path: pathPDF),
                    type: PageTransitionType.fade)),


                                  child:
                                  SvgPicture.asset(
                                    'assets/moon.svg',
                                    height: 120,
                                  )

                              ),
                              Text(
                                'Arbo-lijst',
                                style: cardTextStyle,
                              )
                            ],
                          ),
                        ),


                        Card(
                          color: Colors.grey.shade100,
                          shape:RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(8)
                          ),
                          elevation: 4,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              GestureDetector(
                                  onTap:
                                      () =>
                                      Navigator.of(context).push(PageTransition(
                                          child: todolist(),
                                          type: PageTransitionType.fade)),


                                  child:
                                  SvgPicture.asset(
                                    'assets/moon.svg',
                                    height: 120,
                                  )

                              ),
                              Text(
                                "To do lijst",
                                style: cardTextStyle,
                              )
                            ],
                          ),
                        ),

                        Card(
                          color: Colors.grey.shade100,
                          shape:RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(8)
                          ),
                          elevation: 4,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              GestureDetector(
                                  onTap:
                                      () =>
                                      Navigator.of(context).push(PageTransition(
                                          child: home(),
                                          type: PageTransitionType.fade)),


                                  child:
                                  SvgPicture.asset(
                                    'assets/moon.svg',
                                    height: 120,
                                  )

                              ),
                              Text(
                                'Uitzetlijst',
                                style: cardTextStyle,
                              )
                            ],
                          ),
                        ),

                        Card(
                          color: Colors.grey.shade100,
                          shape:RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(8)
                          ),
                          elevation: 4,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              GestureDetector(
                                  onTap:
                                      () =>
                                      Navigator.of(context).push(PageTransition(
                                          child: inschrijven(),
                                          type: PageTransitionType.fade)),
                                  child:
                                  SvgPicture.asset(
                                    'assets/moon.svg',
                                    height: 120,
                                  )
                              ),
                              Text(
                                'Inschrijven',
                                style: cardTextStyle,
                              )
                            ],
                          ),
                        ),

                        Card(
                          color: Colors.grey.shade100,
                          shape:RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(8)
                          ),
                          elevation: 4,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,

                            children: <Widget>[
                              GestureDetector(
                                  onTap:
                                      () =>
                                      Navigator.of(context).push(PageTransition(
                                          child: home(),
                                          type: PageTransitionType.fade)),


                                  child:
                                  SvgPicture.asset(
                                    'assets/moon.svg',
                                    height: 120,
                                  )

                              ),
                              Text(
                                'Opstart advies',
                                style: cardTextStyle,
                              )

                            ],
                          ),
                        ),

                        Card(
color: Colors.grey.shade100,
                          shape:RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(8)
                          ),

                          elevation: 4,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              GestureDetector(
                                  onTap:
                                      () =>
                                      Navigator.of(context).push(PageTransition(
                                          child: contactpage(),
                                          type: PageTransitionType.fade)),


                                  child:
                                  SvgPicture.asset(
                                    'assets/moon.svg',
                                    height: 120,
                                  )

                              ),
                              Text(

                                'Contact',
                                style: cardTextStyle,
                              )
                            ],
                          ),
                        ),
                        Card(
                          color: Colors.grey.shade100,
                          shape:RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(8)
                          ),
                          elevation: 4,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              GestureDetector(
                                  onTap:
                                      () =>
                                      Navigator.of(context).push(PageTransition(
                                          child: quiz_screen(),
                                          type: PageTransitionType.fade)),


                                  child:
                                  SvgPicture.asset(
                                    'assets/moon.svg',
                                    height: 120,
                                  )

                              ),
                              Text(

                                'Quiz',
                                style: cardTextStyle,
                              )
                            ],
                          ),
                        ),
                        Card(
                          color: Colors.grey.shade100,
                          shape:RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(8)

                          ),
                          elevation: 4,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              GestureDetector(
                                  onTap:
                                      () =>
                                      Navigator.of(context).push(PageTransition(
                                          child: home(),
                                          type: PageTransitionType.fade)),


                                  child:
                                  SvgPicture.asset(
                                    'assets/moon.svg',
                                    height: 120,
                                  )

                              ),

                              Text(
                                'Kaart',
                                style: cardTextStyle,
                              )
                            ],
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

}```

>Solution :

You need define name out of build method to access it in all your class:

String name = ''; //<-- define this

Widget build(BuildContext context) {
    DocumentReference documentReference = FirebaseFirestore.instance.collection(uid!).doc("name");
    documentReference.get().then((datasnapshot){

      name = datasnapshot.data().toString();//<-- change this
    });

 ...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading