我有一个颤动的页面(用户)有两个底部导航栏(用户信息和讨论) . 我通过用户列表转到此页面并将其传递给选择的用户ID . 然后页面查询用户信息,然后将用户信息传递到用户信息页面并讨论到讨论页面 .

class UserView extends StatefulWidget {

final String userUUID;

UserView({this.userUUID});

@override

State createState() {

return _UserViewState();

}

}

class _UserViewState extends State {

int _currentIndex = 0;

String _userUUID;

UserView _user;

List _children;

@override

void initState() {

super.initState();

_userUUID = widget.userUUID;

_children = [

UserInfo(user: _user),

UserComments()

];

}

@override

Widget build(BuildContext context) {

return Scaffold(

appBar: AppBar(

title: Text(_user.userFirstName + " " + _user.userLastName),

),

body: new FutureBuilder(

future: _getUserInfo(http.Client(), _userUUID),

builder: (BuildContext context, AsyncSnapshot snapshot){

if(snapshot.hasError) print(snapshot.error);

return snapshot.hasData ?

UserInfo(user: snapshot.data):

Center(child: CircularProgressIndicator());

}

),

bottomNavigationBar: BottomNavigationBar(

currentIndex: _currentIndex, // this will be set when a new tab is tapped

items: [

BottomNavigationBarItem(

icon: new Icon(Icons.info),

title: new Text('Info'),

),

BottomNavigationBarItem(

icon: new Icon(Icons.chat),

title: new Text('Discussion'),

)

],

onTap: (int index) {

setState(() {

_currentIndex = index;

});

},

),

);

}

}

Future _getUserInfo(http.Client client, String userUUID) async {

final SharedPreferences prefs = await SharedPreferences.getInstance();

Map headersMap = {

'Authorization' : 'Bearer ' + prefs.getString("TOKEN")

};

final response = await http.get(

'https://myapi/user/view/' + userUUID,

headers: headersMap

);

if(response.statusCode == 200) {

return User.fromJson(json.decode(response.body));

} else {

throw Exception('Failed to load user data');

}

}

我正在使用FutureBuilder从API获取信息,并希望将User对象传递到User Info页面,将Discussion list传递给Discussion页面 .

服务器的响应如下所示:

{

"uuid": "829998e8-6a48-4a50-a7e0-3581aeaaee87",

"first_name": "John",

"last_name": "Doe",

"age": 22,

"gender": "Male",

"phone": "",

"email": "",

"created_at": "2018-10-09T00:44:04.000Z",

"updated_at": "2018-10-09T00:44:04.000Z",

"comments": [

{

"uuid": "d2c046c8-efd4-4dfa-bcef-5ee021192a7e",

"user_uuid": "829998e8-6a48-4a50-a7e0-3581aeaaee87",

"comment": "this is comment",

"created_at": "2018-10-16T23:23:25.000Z",

"updated_at": "2018-10-16T23:23:25.000Z"

}

]

}

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐