开发者社区> 问答> 正文

用颤振方法求取一些变量值

我的代码:

import 'package:flutter/material.dart';
//My backend file
import '...';

class UserProfile extends StatefulWidget {
  final String userName;
  final String password;
  final String token;
  UserProfile(this.userName, this.password, this.token, {Key key})
      : super(key: key);
  _UserProfileState createState() => _UserProfileState();
}

class _UserProfileState extends State<UserProfile> {

Future _getUserInfo() async {
var user = await GetUserInfo.getInfo(
    widget.userName, widget.password, widget.token);

final String firstName = user.firstName;
final String email = user.email;
final String cpf = user.cpf;


}

所以我需要什么(我不知道该怎么做)?

1-获取这些“完全名称”、“电子邮件”和“CPF”字符串;

2-将它们设置为一些文本小部件。

我的问题是什么?

不使用局部变量‘block chainId’的值。尝试删除变量,或使用它.DAT(未使用的_局部变量)

我不知道如何从方法中获取这些值,并让外部代码使用它们的值.我知道我需要某种回报,否则他们是无法访问的,但我不知道如何构建这个回报。

所有内部字符串都有有效值,如果我试图将它们打印到方法中,我将看到我需要的是什么

展开
收起
游客5akardh5cojhg 2019-12-24 13:14:21 1998 0
1 条回答
写回答
取消 提交回答
  • 您可以复制粘贴,运行下面的完整代码 步骤1:假设您的getUserInfo()返回aUser对象 步骤2:使用bool loading若要控制加载状态,请在延迟5秒内进行模拟。 步骤3:使用User对象在文本小部件中的信息

    工作演示

    enter image description here

    代码段

    class User { String firstName; String email; String cpf;

      User({this.firstName, this.email, this.cpf});
    }
    

    ... class _UserProfileState extends State { User user; bool loading = false;

    Future _getUserInfo() async { setState(() { loading = true; }); ... @override void initState() {
    _getUserInfo(); super.initState(); } ... return loading ? Center( child: CircularProgressIndicator(), ) : Stack( children: [ 全码

    import 'package:flutter/material.dart'; import 'dart:ui' as ui;

    void main() => runApp(MyApp());

    class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Profile Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: UserProfile(title: 'Profile'), ); } }

    class User { String firstName; String email; String cpf;

    User({this.firstName, this.email, this.cpf}); }

    class GetUserInfo { static Future getInfo( String userName, String password, String token) async { await Future.delayed(Duration(seconds: 5)); return User(firstName: "abc", email: "def", cpf: "123"); } }

    class UserProfile extends StatefulWidget { UserProfile({Key key, this.title, this.userName, this.password, this.token}) : super(key: key); final String title; final String userName; final String password; final String token;

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

    class _UserProfileState extends State { User user; bool loading = false;

    Future _getUserInfo() async { setState(() { loading = true; });

    user = await GetUserInfo.getInfo(
        widget.userName, widget.password, widget.token);
    
    setState(() {
      loading = false;
    });
    

    }

    @override void initState() { // TODO: implement initState _getUserInfo(); super.initState(); }

    @override Widget build(BuildContext context) { final _width = MediaQuery.of(context).size.width; final _height = MediaQuery.of(context).size.height; final String imgUrl = 'https://pixel.nymag.com/imgs/daily/selectall/2017/12/26/26-eric-schmidt.w700.h700.jpg';

    return loading
        ? Center(
            child: CircularProgressIndicator(),
          )
        : Stack(
            children: <Widget>[
              Container(
                color: Colors.blue,
              ),
              Image.network(
                imgUrl,
                fit: BoxFit.fill,
              ),
              BackdropFilter(
                  filter: ui.ImageFilter.blur(
                    sigmaX: 6.0,
                    sigmaY: 6.0,
                  ),
                  child: Container(
                    decoration: BoxDecoration(
                      color: Colors.blue.withOpacity(0.9),
                      borderRadius: BorderRadius.all(Radius.circular(50.0)),
                    ),
                  )),
              Scaffold(
                  appBar: AppBar(
                    title: Text(widget.title),
                    centerTitle: false,
                    elevation: 0.0,
                    backgroundColor: Colors.transparent,
                  ),
                  drawer: Drawer(
                    child: Container(),
                  ),
                  backgroundColor: Colors.transparent,
                  body: Center(
                    child: Column(
                      children: <Widget>[
                        SizedBox(
                          height: _height / 12,
                        ),
                        CircleAvatar(
                          radius: _width < _height ? _width / 4 : _height / 4,
                          backgroundImage: NetworkImage(imgUrl),
                        ),
                        SizedBox(
                          height: _height / 25.0,
                        ),
                        Text(
                          user.firstName,
                          style: TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: _width / 15,
                              color: Colors.white),
                        ),
                        Padding(
                          padding: EdgeInsets.only(
                              top: _height / 30,
                              left: _width / 8,
                              right: _width / 8),
                          child: Text(
                            user.cpf,
                            style: TextStyle(
                                fontWeight: FontWeight.normal,
                                fontSize: _width / 25,
                                color: Colors.white),
                            textAlign: TextAlign.center,
                          ),
                        ),
                        Divider(
                          height: _height / 30,
                          color: Colors.white,
                        ),
                        Row(
                          children: <Widget>[
                            rowCell(343, 'POSTS'),
                            rowCell(673826, 'FOLLOWERS'),
                            rowCell(275, 'FOLLOWING'),
                          ],
                        ),
                        Divider(height: _height / 30, color: Colors.white),
                        Padding(
                          padding: EdgeInsets.only(
                              left: _width / 8, right: _width / 8),
                          child: FlatButton(
                            onPressed: () {},
                            child: Container(
                                child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                Icon(Icons.person),
                                SizedBox(
                                  width: _width / 30,
                                ),
                                Text('FOLLOW')
                              ],
                            )),
                            color: Colors.blue[50],
                          ),
                        ),
                      ],
                    ),
                  ))
            ],
          );
    

    }

    Widget rowCell(int count, String type) => Expanded( child: Column( children: [ Text( '$count', style: TextStyle(color: Colors.white), ), Text(type, style: TextStyle(color: Colors.white, fontWeight: FontWeight.normal)) ], ));

    2019-12-24 13:14:54
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载