gpt4 book ai didi

android - 使用 Flutter 从方法中获取一些变量值

转载 作者:行者123 更新时间:2023-11-28 23:22:17 27 4
gpt4 key购买 nike

我的代码:

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;


}

//As it didn't was clear enough I will show what I want to do

print(_cpf); // <- But this wont work

那么我需要什么(但我不知道该怎么做)?

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

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

我的问题是什么?

未使用局部变量“blockchainId”的值。尝试删除变量,或使用它。dart(unused_local_variable)

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

所有内部字符串都有有效值,如果我尝试打印它们(到方法中),我会正确地看到我需要什么。

最佳答案

您可以在下面复制粘贴运行完整代码
第 1 步:假设您的 getUserInfo() 返回一个 User 对象
第 2 步:使用 bool loading 控制加载状态,模拟延迟 5 秒
第 3 步:在文本小部件

中使用 User 对象的信息

工作演示

enter image description here

代码 fragment

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

User({this.firstName, this.email, this.cpf});
}
...
class _UserProfileState extends State<UserProfile> {
User user;
bool loading = false;

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

完整代码

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<User> 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<UserProfile> {
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: <Widget>[
Text(
'$count',
style: TextStyle(color: Colors.white),
),
Text(type,
style:
TextStyle(color: Colors.white, fontWeight: FontWeight.normal))
],
));
}

关于android - 使用 Flutter 从方法中获取一些变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59463124/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com