您好,我想将信息从其所在的构造函数传递到小部件中。
但是它返回错误:
“只能在初始化程序中访问静态成员”。
此行返回错误:
Upload(currentUsers: widget.currentUsers,),
请帮助我
这是我的代码。
import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:junk_stores/Curved4/Notifications.dart';
import 'package:junk_stores/Curved4/Payments.dart';
import 'package:junk_stores/Curved4/profilePage.dart';
import 'dart:ui' as ui;
import 'package:junk_stores/Curved4/homeScreen.dart';
import 'package:junk_stores/Curved4/uploadPage.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:junk_stores/models/user.dart';
import 'package:junk_stores/pages/home.dart';
import 'package:junk_stores/pages/search.dart';
import 'package:junk_stores/pages/upload.dart';
class BottomNavbar extends StatefulWidget {
final User currentUsers;
BottomNavbar({@required this.currentUsers}){
print(currentUsers);
}
@override
_BottomNavbarState createState() => _BottomNavbarState();
}
class _BottomNavbarState extends State<BottomNavbar> {
int _currentIndex = 0;
final List<Widget> _children = [
HomeS(),
Search(),
Upload(currentUsers: widget.currentUsers,), // the error is here
Notifications(),//trending and notifications page
Profile(),
];
void onTappedBar (int index) {
setState(() {
_currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _children[_currentIndex],
bottomNavigationBar: CurvedNavigationBar(
color: Colors.white,
onTap : onTappedBar,
buttonBackgroundColor: Colors.white,
backgroundColor: Colors.purple[400],
animationCurve: Curves.ease,
animationDuration: Duration(milliseconds: 700),
index: 0,
height: MediaQuery.of(context).size.height * 0.07407407,//52 pixels
items: <Widget>[
ShaderMask(
blendMode: BlendMode.srcIn,
shaderCallback: (Rect bounds) {
return ui.Gradient.linear(
Offset(4.0, 24.0),
Offset(24.0, 4.0),
[
Colors.red[600],
Colors.purple,
],
);
},
child: Icon(
FontAwesomeIcons.home,
size: 24,
),
),
ShaderMask(
blendMode: BlendMode.srcIn,
shaderCallback: (Rect bounds) {
return ui.Gradient.linear(
Offset(4.0, 24.0),
Offset(24.0, 4.0),
[
Colors.red[600],
Colors.purple,
],
);
},
child: Icon(
FontAwesomeIcons.search,
size: 24,
),
),
ShaderMask(
blendMode: BlendMode.srcIn,
shaderCallback: (Rect bounds) {
return ui.Gradient.linear(
Offset(4.0, 24.0),
Offset(24.0, 4.0),
[
Colors.red[600],
Colors.purple,
],
);
},
child: Icon(
FontAwesomeIcons.plus,
size: 24,
),
),
ShaderMask(
blendMode: BlendMode.srcIn,
shaderCallback: (Rect bounds) {
return ui.Gradient.linear(
Offset(4.0, 24.0),
Offset(24.0, 4.0),
[
Colors.red[600],
Colors.purple,
],
);
},
child: Icon(
Icons.trending_up,
size: 29,
),
),
ShaderMask(
blendMode: BlendMode.srcIn,
shaderCallback: (Rect bounds) {
return ui.Gradient.linear(
Offset(4.0, 24.0),
Offset(24.0, 4.0),
[
Colors.red[600],
Colors.purple,
],
);
},
child: Icon(
FontAwesomeIcons.user,
size: 24,
)
)
],
),
);
}
}
我曾尝试阅读其他解决方案,但我似乎并没有理解。我只想将构造函数中的信息传递给Upload Widget。 ................................................... ...................
这是我的上载代码的顶部。
class Upload extends StatefulWidget {
final User currentUsers;
Upload({@required this.currentUsers}){
print(currentUsers);
}
@override
_UploadState createState() => _UploadState();
}
class _UploadState extends State<Upload> {
buildSplashSreen(){
return Scaffold(
appBar: AppBar(
答案 0 :(得分:1)
在初始化状态之前,您无法访问“ widget”(widget.currentUsers)。
类似的东西:
class _BottomNavbarState extends State<BottomNavbar> {
int _currentIndex = 0;
List<Widget> _children;
@override
initState() {
_children = [
HomeS(),
Search(),
Upload(currentUsers: widget.currentUsers,),
Notifications(),
Profile(),
]
}
...
}
或作为吸气剂
List<Widget> get _children => [
HomeS(),
Search(),
Upload(currentUsers: widget.currentUsers,),
Notifications(),
Profile(),
];
答案 1 :(得分:0)
尝试这样:
MyWidget(currentUsers: someuser);
class MyWidget extends StatefulWidget {
final User currentUsers;
const MyWidget({Key key, this.currentUsers}) : super(key: key);
@override
_MyWidgetState createState() => _MyWidgetState(this.currentUsers);
}
class _MyWidgetState extends State<MyWidget> {
final User currentUsers;
_MyWidgetState(this.currentUsers);
@override
Widget build(BuildContext context) {
return Container(
);
}
}