我嘗試為我的應用程式制作一個簡單的組態檔頁面,但我不知道該怎么做。
我想打電話時出錯
'${widget.user['first_name']}'
在我的個人資料小部件中:
NoSuchMethodError (NoSuchMethodError: The method '[]' was called on null. Receiver: null Tried calling: []("first_name"))
我的主頁是一個包含 2 個小部件的 PageView。
// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'dart:convert';
import 'package:clic_ads/Login/apiClient.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../Composants/appbar.dart';
import '../Login/login.dart';
import 'notifs.dart';
import 'profil.dart';
class MainPage extends StatefulWidget {
final dynamic user;
const MainPage({super.key, this.user});
@override
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
final Api _apiClient = Api();
dynamic user;
final PageController _controller = PageController(
initialPage: 0,
);
int activePage = 0;
@override
Future<void> dispose() async {
_controller.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
}
void bottomTapped(int index) {
setState(() {
activePage = index;
_controller.animateToPage(index,
duration: Duration(milliseconds: 500), curve: Curves.ease);
});
}
@override
Widget build(BuildContext context) {
Future<void> getUser() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var token = prefs.getString('token');
this.user = await _apiClient.user(token!);
print(user);
return user ;
}
getUser();
return Scaffold(
appBar: PreferredSize(
preferredSize:
Size.fromHeight(MediaQuery.of(context).size.height * 0.07),
child: TopAppbar()),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: activePage,
backgroundColor: const Color(0xffC64C4E),
selectedItemColor: Colors.white,
unselectedItemColor: Colors.white.withOpacity(.55),
selectedFontSize: 14,
unselectedFontSize: 14,
onTap: (value) {
bottomTapped(value);
},
items: [
BottomNavigationBarItem(
label: 'Notifs',
icon: Icon(Icons.view_list),
),
BottomNavigationBarItem(
label: 'Profil',
icon: Icon(
Icons.account_circle,
),
),
],
),
backgroundColor: const Color(0xffEEEEEE),
body: PageView(
onPageChanged: (index) {
setState(() {
activePage = index;
});
},
controller: _controller,
children: [Notifs(), Profil(user: this.user)],
),
);
}
}
我的第一個小部件是顯示通知的小部件:
// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables, unnecessary_new
import 'package:clic_ads/Login/apiClient.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Notifs extends StatefulWidget {
final dynamic user;
const Notifs({super.key, this.user});
@override
State\<Notifs\> createState() =\> \_NotifsState();
}
class \_NotifsState extends State\<Notifs\> {
final Api \_apiClient = Api();
dynamic user;
@override
Widget build(BuildContext context) {
double deviceWidth = MediaQuery.of(context).size.width;
double deviceHeight = MediaQuery.of(context).size.height;
Future\<void\> getUser() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var token = prefs.getString('token');
this.user = await \_apiClient.user(token!);
print(user);
return user ;
}
getUser();
return ListView.builder(
itemCount: 10,
itemBuilder: (BuildContext context, int index) {
return Column(
children: [
new Container(
decoration: new BoxDecoration(
color: Color.fromARGB(255, 255, 255, 255),
borderRadius: new BorderRadius.only(
topRight: const Radius.circular(15.0),
topLeft: const Radius.circular(15.0),
)),
margin: EdgeInsets.only(
top: deviceWidth * 0.03,
right: deviceWidth * 0.05,
left: deviceWidth * 0.05),
height: deviceHeight * 0.148,
alignment: Alignment.centerLeft,
child: Row(
children: [
Container(
decoration: new BoxDecoration(
color: const Color(0xffC64C4E),
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(15.0),
)),
width: deviceWidth * 0.12,
child: Center(
child: FittedBox(
child: Text(
'CU',
style: TextStyle(
fontSize: deviceWidth * 0.06,
fontWeight: FontWeight.bold,
color: Colors.white),
),
),
),
),
Padding(
padding: EdgeInsets.all(deviceWidth * 0.02),
child: SizedBox(
width: deviceWidth * 0.73,
child: Column(
children: [
Row(
children: [
Text('17:24',
style: TextStyle(
fontSize: deviceWidth * 0.04,
color: const Color(0xffC64C4E),
fontWeight: FontWeight.w600)),
Spacer(),
Text('20/10/2022',
style: TextStyle(
fontSize: deviceWidth * 0.04,
color: const Color(0xffC64C4E),
fontWeight: FontWeight.w600))
],
),
Padding(
padding: EdgeInsets.all(deviceWidth * 0.02),
child: Row(
children: [
Icon(Icons.person),
Text('DUPOND Martin (M.)',
style: TextStyle(
fontSize: deviceWidth * 0.035,
fontWeight: FontWeight.w500))
],
),
),
Padding(
padding: EdgeInsets.all(deviceWidth * 0.02),
child: Row(
children: [
Icon(
Icons.book,
),
Text('CU01201222U0009',
style: TextStyle(
fontSize: deviceWidth * 0.035,
fontWeight: FontWeight.w500))
],
),
)
],
),
),
)
],
),
),
Container(
color: const Color(0xffC64C4E),
height: 3,
width: deviceWidth * 0.90)
],
);
},
);
}
}
我想顯示用戶詳細資訊的第二個小部件在這里:
// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'package:clic_ads/Login/apiClient.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Profil extends StatefulWidget {
final dynamic user;
const Profil({super.key, this.user});
@override
State\<Profil\> createState() =\> \_ProfilState();
}
class \_ProfilState extends State\<Profil\> {
// final Api \_apiClient = Api();
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
// Future<void> getUser() async {
// SharedPreferences prefs = await SharedPreferences.getInstance();
// var token = prefs.getString('token');
// user = await _apiClient.user(token!);
// print(user['data']);
// }
// getUser();
// print(user);
double deviceWidth = MediaQuery.of(context).size.width;
double deviceHeight = MediaQuery.of(context).size.height;
return Padding(
padding: EdgeInsets.all(deviceWidth * 0.05),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Profil',
style: TextStyle(
fontSize: deviceWidth * 0.06, fontWeight: FontWeight.w500)),
Padding(
padding: EdgeInsets.only(left: deviceWidth * 0.05),
child: Row(
children: [
Icon(
Icons.account_circle,
size: deviceWidth * 0.3,
),
Padding(
padding: EdgeInsets.only(left: deviceWidth * 0.05),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Text('${widget.user['first_name']}',
style: TextStyle(
fontSize: deviceWidth * 0.04,
fontWeight: FontWeight.w500)),
SizedBox(width: 10),
Text('${widget.user['last_name']}',
style: TextStyle(
fontSize: deviceWidth * 0.04,
fontWeight: FontWeight.w500))
],
),
SizedBox(
height: 15,
),
Text('${widget.user['email']}',
style: TextStyle(
fontSize: deviceWidth * 0.04,
fontWeight: FontWeight.w500))
],
),
)
],
),
),
Text('Paramètres',
style: TextStyle(
fontSize: deviceWidth * 0.06, fontWeight: FontWeight.w500))
],
),
);
}
}
我希望在我的個人資料頁面上顯示我的用戶資訊
uj5u.com熱心網友回復:
歡迎來到 StackOverflow!
在'${widget.user['first_name']}'第二個代碼示例的行中,從哪里獲取用戶資訊?對我來說,似乎沒有當前用戶的實體可以從中獲取資訊。
uj5u.com熱心網友回復:
嘗試接受 null like
'${widget.user?['first_name']}'
uj5u.com熱心網友回復:
該getUser();方法是異步方法,但在_MainPageState.build方法執行期間不等待。
所以當children: [Notifs(), Profil(user: this.user)],line 被執行時,user類成員沒有被初始化。
因此,您可能希望使用FutureBuilder來等待未來并獲得結果。
該解決方案需要getUser將方法調整為類方法,并回傳Future<dynamic>稍后使用的值:
class _NotifsState extends State<Notifs> {
...
Future<dynamic> getUser() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var token = prefs.getString('token');
this.user = await _apiClient.user(token!);
print(user);
return user ;
}
@override
Widget build(BuildContext context) {
...
children: [Notifs(), FutureBuilder<dynamic>(
future: getUser(),
builder: (c, snapshot) => snapshot.hasData
? Profil(user: snapshot.data)
: const CircularProgressIndicator())
],
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/522611.html
上一篇:FlutterMethodChannel是如何作業的,它與Interop有何不同?
下一篇:Flutter-復制物件串列
