Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
7.6k views
in Technique[技术] by (71.8m points)

firebase - The argument type 'Future<dynamic>' can't be assigned to the parameter type 'String'

I've seen similar questions that were asked in regards to this but my problem is a little different in that I'm keeping my application modular so I have defined the following method in a different dart file(Simply a class, not a widget):

Future getProfile() async {
return await usersCollection.doc(uid).get().then<dynamic>((DocumentSnapshot snapshot) async {
  print(snapshot.data()['name']);
  if(snapshot.data()['name'] == null){
    print("No name exists");
  }
  else {
    return snapshot.data()['name'];
  }
});

And I'm trying to use it's value on my home widget:

import 'package:flutter/material.dart';
import 'package:carpoolapp/services/auth.dart';
import 'package:carpoolapp/services/database.dart';
import 'package:firebase_auth/firebase_auth.dart';

class Home extends StatefulWidget {

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

class _HomeState extends State<Home> {
  final AuthService _auth = AuthService();

  User user = FirebaseAuth.instance.currentUser;

  DatabaseService db = DatabaseService(uid: FirebaseAuth.instance.currentUser.uid);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.redAccent,
        appBar: AppBar(
          title: Text('Signed in'),
          backgroundColor: Colors.blueAccent,
          elevation: 0.0, //no drop shadow
          actions: <Widget>[
            FlatButton.icon(
                onPressed: () async {
                  await _auth.signOutUser();
                },
                icon: Icon(Icons.person),
                label: Text('logout')),
          ],
        ),
        body: Text(db.getProfile()), // Error is here
        //body: UserTile(user: FirebaseAuth.instance().getCurrentUser()),
    );
  }
}

How do I go about making this work without sacrificing the modularity?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
 
Best answer

By seeing

The argument type 'Future<dynamic>' can't be assigned to the parameter type 'String'

this and

Text(db.getProfile())

the issue is db.getProfile() is an async method. That's why its telling Future can't be assigned to String since Text widget data key is of type String not Future<String>.

You can use FutureBuilder in the body and use the snapshot in the Text which will have the String value.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...