如何在颤振中显示来自firebase的用户名?

时间:2021-06-23 22:02:06

标签: flutter firebase-realtime-database

我正在尝试在 Text() 中显示我用户的全名。到目前为止,我已经尝试添加 $fullname 但这没有用。我的数据库在 Firebase 实时数据库中。而且我也尝试过使用提供程序,但创建了无法找到的错误,例如:'${Provider.of< Driver >(context).fullName}',

这里是我想打印的地方:

Column(
         mainAxisAlignment: MainAxisAlignment.center,
         children: <Widget>[
             Text('$fullName',style: TextStyle(fontSize: 20, fontFamily: 'Brand-Bold'),
             ),

完整代码如下:

import 'package:cabdriver/datamodels/driver.dart';
import 'package:cabdriver/globalvariabels.dart';
import 'package:cabdriver/screens/about.dart';
import 'package:cabdriver/screens/registration.dart';
import 'package:cabdriver/screens/security.dart';
import 'package:cabdriver/screens/support.dart';
import 'package:cabdriver/widgets/BrandDivier.dart';
import 'package:cabdriver/widgets/ProgressDialog.dart';
import 'package:cabdriver/widgets/TaxiButton.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:outline_material_icons/outline_material_icons.dart';
import 'package:provider/provider.dart';

class ProfileTab extends StatefulWidget {
  @override
  _ProfileTabState createState() => _ProfileTabState();
}

class _ProfileTabState extends State<ProfileTab> {
  void logout() async {
    //show dialog
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (BuildContext context) => ProgressDialog(
        status: 'Logging Out',
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: SafeArea(
        child: Padding(
          padding: EdgeInsets.all(8),
          child: Column(
            children: <Widget>[
              Container(
                  color: Colors.white,
                  height: 160,
                  child: DrawerHeader(
                    decoration: BoxDecoration(color: Colors.white),
                    child: Row(
                      children: <Widget>[
                        Image.asset(
                          'images/user_icon.png',
                          height: 60,
                          width: 60,
                        ),
                        SizedBox(
                          width: 15,
                        ),
                        Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Text(
                              '$fullName',
                              style: TextStyle(
                                  fontSize: 20, fontFamily: 'Brand-Bold'),
                            ),
                            SizedBox(
                              height: 5,
                            ),
                          ],
                        )
                      ],
                    ),
                  )),
              BrandDivider(),
              SizedBox(
                height: 10,
              ),
              ListTile(
                leading: Icon(
                  OMIcons.settings,
                  color: Colors.redAccent,
                ),
                title: Text(
                  'Account Settings',
                ),
                onTap: () {},
              ),
              ListTile(
                leading: Icon(
                  OMIcons.creditCard,
                  color: Colors.redAccent,
                ),
                title: Text(
                  'Payments',
                ),
                onTap: () {},
              ),
              ListTile(
                leading: Icon(
                  OMIcons.security,
                  color: Colors.redAccent,
                ),
                title: Text(
                  'Security',
                ),
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => SecurityPage(),
                    ),
                  );
                },
              ),
              ListTile(
                leading: Icon(
                  OMIcons.contactSupport,
                  color: Colors.redAccent,
                ),
                title: Text(
                  'Support',
                ),
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => SupportPage(),
                    ),
                  );
                },
              ),
              ListTile(
                leading: Icon(
                  OMIcons.info,
                  color: Colors.redAccent,
                ),
                title: Text('About'),
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => AboutPage(),
                    ),
                  );
                },
              ),
              SizedBox(
                height: 100,
              ),
              TaxiButton(
                title: 'LOGOUT',
                color: Colors.red,
                onPressed: () {
                  logout();
                  Navigator.pushNamedAndRemoveUntil(
                      context, RegistrationPage.id, (route) => false);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

这是我的驱动程序的 dart 文件,其中存储了全名和其他详细信息

import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/widgets.dart';

class Driver {
  String fullName;
  String email;
  String phone;
  String id;
  String carModel;
  String carColor;
  String vehicleNumber;

  Driver({
    this.fullName,
    this.email,
    this.phone,
    this.id,
    this.carModel,
    this.carColor,
    this.vehicleNumber,
  });

  Driver.fromSnapshot(DataSnapshot snapshot) {
    id = snapshot.key;
    phone = snapshot.value['phone'];
    email = snapshot.value['email'];
    fullName = snapshot.value['fullname'];
    carModel = snapshot.value['vehicle_details']['car_model'];
    carColor = snapshot.value['vehicle_details']['car_color'];
    vehicleNumber = snapshot.value['vehicle_details']['vehicle_number'];
  }
}

0 个答案:

没有答案