1

我正在尝试在没有 ListView 的配置文件页面上显示 Laravel Api 响应数据,在该页面中我正在获取 api 响应,但无法在 Flutter 中显示没有 ListView.builder 的数据,甚至无法在 setState() 方法中定义响应关于 。

我已经从 api 获取了所有用户配置文件的详细信息。

我的代码如下所示:

 class ProfilePage extends StatefulWidget {

  final String scratchCard;

  const ProfilePage({Key key,  this.scratchCard}) : super(key: key);
  @override
  _ProfilePageState createState() => _ProfilePageState( scratchCard);
}

class _ProfilePageState extends State<ProfilePage> {

  //  Map profile = jsonDecode(result.body);

  //  List<Map<String, dynamic>> profile;
  bool isLoading = false;
  String student_name;
  bool _visible = false;
  String scratchCard;
  String text;
  String user;
  String email;
  String institute;
  String mobile;
  String referral_code;
  String reg_no;
  String name;
  String first_name;
  String last_name;
  String subject_name;
  String standard_name;
  String icon;

  var institute_mobile;
  var institute_icon;
  var subject;
  var standard;
  var student;

  bool premium;


  _ProfilePageState(this.scratchCard);

  @override
  void initState() {
    super.initState();
    this.getProfileDetails();
  }

  @override
  void dispose() {
    super.dispose();
  }

  getProfileDetails() async {
    setState(() {
      isLoading = true;
    });

    var response = await CallApi().getProfile();
    print(response.body);
    if(response.statusCode == 200){
      var items = json.decode(response.body)['user'];
      setState(() {
        //       institute = items.student[0].institute.name;

        student_name = items['user']['first_name'] + ' ' + items['user']['last_name'];
        institute = items['student'][0]['institute']['name'];
        email = items['user']['email'];
        mobile = items['user']['mobile'];
        referral_code = items['user']['referral_code'];
        reg_no = items['student'][0]['reg_no'];
        institute_mobile = items['student'][0]['institute']['user']['mobile'];
        institute_icon = items['student'][0]['institute']['icon'];
        subject=items.subject_name;

        this.standard=items['subject_name']['standard_name'];

        isLoading = false;
      });
    }else{
      //      tableData = [];
      isLoading = false;
    }
  }
  @override
  Widget build(BuildContext context) {

    var subject;
    return Scaffold(
      extendBodyBehindAppBar: true,
      body: Stack(
          children: <Widget>[
            Container(
              height: 275,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.vertical(bottom: Radius.circular(30.0)),
                color: Constants.blueLight,
              ),
              child: Stack(
                children: <Widget>[
                  Positioned(
                    child: Image.asset("assets/images/blob_2.png",
                        width: 332,
                        height: 323,
                        color: Constants.blueDark),
                    top: -80,
                    right: -110,
                  ),
                  Positioned(
                    child: Image.asset("assets/images/blob_1.png",
                        width: 255,
                        height: 267,
                        color: Constants.blueMain),
                    top: 20,
                    left: -80,
                  ),
                ],
              ),
            ),
//          HeaderInner(),
            ListView(
                scrollDirection: Axis.vertical,
                physics: NeverScrollableScrollPhysics(),
                shrinkWrap: true,
                children: <Widget>[
                  Container(
//                color: Colors.blue[600],
//                height: 40 * SizeConfig.heightMultiplier,
                    height: 100 ,
                    child: Padding(
                      padding:  EdgeInsets.only(left: 20.0, right: 20.0, top: 0 ),
                      child: Column(
                        children: <Widget>[
                          Row(
                            children: <Widget>[
                              Container(
                                height:60, width: 60,
//                            height: 11 * SizeConfig.heightMultiplier,
//                            width: 22 * SizeConfig.widthMultiplier,
                                decoration: BoxDecoration(
                                    shape: BoxShape.circle,
                                    image: DecorationImage(
                                        fit: BoxFit.cover,
                                        image: NetworkImage('https://github.com/slackvishal/flutter_traveler_profile_app/blob/master/assets/profileimg.png?raw=true'))
                                ),
                              ),
                              SizedBox(width: 10,),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Text(student_name.toString(), style: TextStyle(
                                      color: Colors.white,
//                                  fontSize: 3 * SizeConfig.textMultiplier,
                                      fontWeight: FontWeight.bold, fontSize: 16
                                  ),),
                                  Text(email.toString(), style: TextStyle(
                                      color: Colors.white70,fontSize: 13
//                                fontSize: 1.9 * SizeConfig.textMultiplier,
                                  ),),
                                ],
                              ),
       
                            ],
                          ),
                          // SizedBox(height: ),
                          Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: <Widget>[

                              Container(
                                decoration: BoxDecoration(
                                  border: Border.all(color: Colors.white60),
                                  borderRadius: BorderRadius.circular(5.0),
                                ),
                                child: Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Text("EDIT PROFILE", style: TextStyle(
                                    color: Colors.white60,
                                  ),),
                                ),
                              ),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ),
                  Container(
                    height: 550,
                    padding: EdgeInsets.fromLTRB(
                        Constants.mainPadding,
                        Constants.mainPadding * 1.5,
                        Constants.mainPadding,
                        Constants.mainPadding
                    ),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.vertical(top: Radius.circular(50.0)),
                      color: Colors.white,
                    ),
                    child:ListView(
                      scrollDirection: Axis.vertical,
                      shrinkWrap: true,
                      children: [
                        InkWell(
                          onTap: () {
                            Navigator.push(
                                context, MaterialPageRoute(builder: (context) => Standard()));
                          },
                          child: Container(
                            width: double.infinity,
                            padding: EdgeInsets.only(left:20.0, right: 20, top: 13, bottom: 13),
                            margin: EdgeInsets.only(bottom: 10),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(30.0),
                              color: Constants.lightPink,
                            ),
                            child: Row(
                              children: <Widget>[
                                Icon(Icons.location_city, color: Constants.textDark, size: 20,),
//                            Image.asset("assets/images/education-logo.jpg", width: 40, height: 40),
                                SizedBox(width: 30.0),
                                Expanded(
                                  child: Row(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    children: <Widget>[
                                      Container(
                                        margin: EdgeInsets.only(top: 0),
                                        child:Text( 'Institute : ' ,
                                          style: TextStyle(
                                              fontSize: 17,
                                              fontWeight: FontWeight.w500,
                                              color: Constants.textDark
                                          ),
                                        ),
                                      ),
                                      SizedBox(width: 5,),
                                      Container(
                                        margin: EdgeInsets.only(top: 2),
                                        child:  Text(institute.toString(),
                                          style: TextStyle(
                                              fontSize: 14,
                                              fontWeight: FontWeight.w400,
                                              color: Constants.textDark
                                          ),
                                        ),
                                      ),
                                    
                                    ],
                                  ),
                                ),

                              ],

                            ),
                          ),
                        ),
                        InkWell(
                            onTap: () {
//                  Navigator.push(
//                      context, MaterialPageRoute(builder: (context) => Standard()));
                            },
                            child: Container(
                              width: double.infinity,
                              padding: EdgeInsets.only(left:20.0, right: 20, top: 13, bottom: 13),
                              margin: EdgeInsets.only(bottom: 10),
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(30.0),
                                color: Constants.lightYellow,
                              ),
                              child: Row(
                                children: <Widget>[
                                  Icon(Icons.phone, color: Constants.textDark, size: 20,),
//                            Image.asset("assets/images/education-logo.jpg", width: 40, height: 40),
                                  SizedBox(width: 30.0),
                                  Expanded(
                                    child: Row(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      mainAxisAlignment: MainAxisAlignment.start,
                                      children: <Widget>[
                                        Container(
                                          margin: EdgeInsets.only(top: 0),
                                          child:Text( "Mobile: ",
                                            style: TextStyle(
                                                fontSize: 17,
                                                fontWeight: FontWeight.w500,
                                                color: Constants.textDark
                                            ),
                                          ),
                                        ),
                                        SizedBox(width: 5,),
                                        Container(
                                          margin: EdgeInsets.only(top: 2),
                                          child: Text(mobile.toString(),
                                            style: TextStyle(
                                                fontSize: 14,
                                                fontWeight: FontWeight.w400,
                                                color: Constants.textDark
                                            ),
                                          ),
                                        ),
                                        SizedBox(height: 5,),
                                      ],
                                    ),
                                  ),

                                ],

                              ),

                            )
                        ),
                        InkWell(
                            onTap: () {
                              Navigator.push(
                                  context, MaterialPageRoute(builder: (context) => Standard()));
                            },
                            child: Container(
                              width: double.infinity,
                              padding: EdgeInsets.only(left:20.0, right: 20, top: 13, bottom: 13),
                              margin: EdgeInsets.only(bottom: 10),
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(30.0),
                                color: Constants.lightBlue,
                              ),
                              child: Row(
                                children: <Widget>[
                                  Icon(Icons.code, color: Constants.textDark, size: 20,),
//                            Image.asset("assets/images/education-logo.jpg", width: 40, height: 40),
                                  SizedBox(width: 30.0),
                                  Expanded(
                                    child: Row(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      mainAxisAlignment: MainAxisAlignment.start,
                                      children: <Widget>[
                                        Container(
                                          margin: EdgeInsets.only(top: 0),
                                          child:Text( "Referral Code: ",
                                            style: TextStyle(
                                                fontSize: 17,
                                                fontWeight: FontWeight.w500,
                                                color: Constants.textDark
                                            ),
                                          ),
                                        ),
                                        SizedBox(width: 5,),
                                        Container(
                                          margin: EdgeInsets.only(top: 2),
                                          child: Text(referral_code.toString(),
                                            style: TextStyle(
                                                fontSize: 14,
                                                fontWeight: FontWeight.w400,
                                                color: Constants.textDark
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              ),

                            )
                        ),
                        InkWell(
                            onTap: () {
                              Navigator.push(
                                  context, MaterialPageRoute(builder: (context) => Standard()));
                            },
                            child: Container(
                              width: double.infinity,
                              padding: EdgeInsets.only(left:20.0, right: 20, top: 13, bottom: 13),
                              margin: EdgeInsets.only(bottom: 10),
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(30.0),
                                color: Constants.lightViolet,
                              ),
                              child: Row(
                                children: <Widget>[
                                  Icon(Icons.confirmation_number, color: Constants.textDark, size: 20,),
//                            Image.asset("assets/images/education-logo.jpg", width: 40, height: 40),
                                  SizedBox(width: 30.0),
                                  Expanded(
                                    child: Row(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      mainAxisAlignment: MainAxisAlignment.start,
                                      children: <Widget>[
                                        Container(
                                          margin: EdgeInsets.only(top: 0),
                                          child:Text( "Registration No.: ",
                                            style: TextStyle(
                                                fontSize: 17,
                                                fontWeight: FontWeight.w500,
                                                color: Constants.textDark
                                            ),
                                          ),
                                        ),
                                        SizedBox(width: 5,),
                                        Container(
                                          margin: EdgeInsets.only(top: 2),
                                          child: Text(reg_no.toString(),
                                            style: TextStyle(
                                                fontSize: 14,
                                                fontWeight: FontWeight.w400,
                                                color: Constants.textDark
                                            ),
                                          ),),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            )
                        ),
                        InkWell(
                            onTap: () {
                              Navigator.push(
                                  context, MaterialPageRoute(builder: (context) => Standard()));
                            },
                            child: Container(
                              width: double.infinity,
                              padding: EdgeInsets.only(left:20.0, right: 20, top: 13, bottom: 13),
                              margin: EdgeInsets.only(bottom: 10),
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(30.0),
                                color: Constants.lightLime,
                              ),
                              child: Row(
                                children: <Widget>[
                                  Icon(Icons.report, color: Constants.textDark),
//                            Image.asset("assets/images/education-logo.jpg", width: 40, height: 40),
                                  SizedBox(width: 30.0),
                                  Expanded(
                                    child: Row(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      mainAxisAlignment: MainAxisAlignment.start,
                                      children: <Widget>[
                                        Container(
                                          margin: EdgeInsets.only(top: 0),
                                          child:Text( "Referral Percentage: ",
                                            style: TextStyle(
                                                fontSize: 17,
                                                fontWeight: FontWeight.w500,
                                                color: Constants.textDark
                                            ),
                                          ),
                                        ),
                                        SizedBox(width: 5,),
                                        Container(
                                          margin: EdgeInsets.only(top: 2),
                                          child: Text("5%",
                                            style: TextStyle(
                                                fontSize: 14,
                                                fontWeight: FontWeight.w400,
                                                color: Constants.textDark
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),

                                ],

                              ),

                            )
                        ),
                       
                   
                      ],
                    ),

                  ),

                ])

          ]),
    );
  }
  }

我在下面收到此错误:

E/flutter ( 8258): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
E/flutter ( 8258): Receiver: null
E/flutter ( 8258): Tried calling: []("first_name")
E/flutter ( 8258): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
E/flutter ( 8258): #1      _ProfilePageState.getProfileDetails.<anonymous closure> (package:spotway_app/screens/profile.dart:100:36)
E/flutter ( 8258): #2      State.setState (package:flutter/src/widgets/framework.dart:1089:30)
E/flutter ( 8258): #3      _ProfilePageState.getProfileDetails (package:spotway_app/screens/profile.dart:97:7)
E/flutter ( 8258): <asynchronous suspension>
4

2 回答 2

1

错误的第三行说Tried calling: []("first_name")
Here the 这[]表明您可能从var items = json.decode(response.body)['user'];
Do your response has key titled as获取空值user
打印您的回复并查看详细结构以映射您所需的键/值。


我用最少的数据尝试过这样的操作,它正在工作,
另外,我已经将数据存储在Map<String, dynamic>.

String getData() {
  String data  = '{"user":{"id":779,"first_name":"test","last_name":"demo","email":"test.demo@gmail.com","mobile":9090909090,"email_verified_at":null,"role_id":5}}';
  Map<String, dynamic> d = jsonDecode(data);
  print(d['user']['id']);
  return d['user']['first_name'];
}
于 2021-04-16T17:57:29.183 回答
0

Flutter 中的代码结构使得您必须为数组索引指定一个数字。

例如 ::

变量名称 = json_decode(response.body)[1]['name']

于 2021-04-17T21:44:14.510 回答