我如何防止ListView显示此错误以显示RangeError(索引)-Flutter

时间:2020-07-21 09:34:23

标签: flutter dart flutter-layout flutter-dependencies flutter-test

我有一个列表,显示来自端点的项目。当我发出HTTP请求时,该请求通过了,但是当我尝试滚动时,我得到了一半的数据,而在尾端,我得到了一个错误。有人可以帮我吗?我想向下滚动时显示的错误如下:

RangerError (index): invalid value: Not in range 0..7, 
inclusive: 8 
See also:

这些方法可以帮助我发出HTTP请求:

Future<void> get_farmer_eop() async {
    SharedPreferences localStorage = await SharedPreferences.getInstance();
    var userJson = localStorage.getString('loginRes');
    user = json.decode(userJson);
    print(user['UserName']);

    final response = await http.get(
        'http://api.ergagro.com:112/GenerateFarmersEop?farmerBvn=${widget.result}&dcOid=${widget.dc_result}&agentName=${user['UserName']}',
        headers: _setHeaders());

    print('${response.statusCode}popo');
    if (response.statusCode == 200) {
      final jsonStatus = jsonDecode(response.body);
      var maineops = jsonStatus['Eop'];
      var eoplines = maineops['EopLines'];
      setState(() {
        q = maineops;
        r = eoplines;
      });
      // print('trandid');
      // print('${q['TransId']}kukuk');
    } else {
      throw Exception();
    }
  }

  _setHeaders() => {
        'Content-type': 'application/json',
        'Accept': 'application/json',
  };

这是完整的代码:

//initializing 
import 'dart:convert';
import 'package:erg_app/StartScan.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:erg_app/models/eopModel.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ProfilePage extends StatefulWidget {
  final String result;
  final String dc_result;

  ProfilePage({this.result, this.dc_result});

  @override
  _ProfilePageState createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  //From Eop Model
  List<EopLine> eops = new List();

  var p;
  var q;
  var r;
  var user;
  var userData;
  var userDetail;
  var anchors;

  @override
  initState() {
    super.initState();
    // _getUserInfo();
    get_farmer();
    get_farmer_eop();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: p == null
          ? Scaffold(
              body: Center(
                child: CircularProgressIndicator(
                  valueColor: AlwaysStoppedAnimation<Color>(Colors.green),
                  backgroundColor: Colors.grey,
                ),
              ),
            )
          : Scaffold(
              appBar: AppBar(
                title: new Center(
                    child: new Text('Farmers Data', textAlign: TextAlign.left)),
                iconTheme: IconThemeData(color: Colors.white),
                backgroundColor: Colors.green,
                leading: new IconButton(
                  icon: new Icon(Icons.assignment_ind),
                  onPressed: () {},
                ),
              ),
              body: Container(
                child: ListView(
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.only(top: 20),
                    ),
                    Padding(
                      padding: EdgeInsets.only(left: 110, right: 110),
                      child: CircleAvatar(
                        radius: 80,
                        backgroundColor: Colors.grey,
                        backgroundImage: NetworkImage(p['PhotoUrl']),
                      ),
                    ),
                    Text(
                      p['FullName'],
                      style: TextStyle(
                        fontFamily: 'SourceSansPro',
                        fontSize: 25,
                      ),
                      textAlign: TextAlign.center,
                    ),
                    Text(
                      p['ApplicationId'],
                      style: TextStyle(
                        fontSize: 18,
                        fontFamily: 'SourceSansPro',
                        color: Colors.green[400],
                        letterSpacing: 2.5,
                      ),
                      textAlign: TextAlign.center,
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 20),
                    ),
                    SizedBox(
                      height: 20.0,
                      width: 200,
                      child: Divider(
                        color: Colors.teal[100],
                      ),
                    ),
                    Text(
                      'Farmer Details',
                      textAlign: TextAlign.center,
                    ),
                    Card(
                      color: Colors.white,
                      margin: EdgeInsets.symmetric(
                          vertical: 10.0, horizontal: 25.0),
                      child: ListTile(
                        leading: Text(
                          'Appication ID:',
                          style: TextStyle(
                            fontSize: 20,
                            fontFamily: 'SourceSansPro',
                            color: Colors.green[700],
                            letterSpacing: 2.5,
                          ),
                        ),
                        title: Text(
                          p['ApplicationId'],
                          style: TextStyle(
                              fontFamily: 'BalooBhai', fontSize: 20.0),
                        ),
                      ),
                    ),

                    Card(
                      color: Colors.white,
                      margin: EdgeInsets.symmetric(
                          vertical: 10.0, horizontal: 25.0),
                      child: ListTile(
                        leading: Text(
                          'BVN:',
                          style: TextStyle(
                            fontSize: 20,
                            fontFamily: 'SourceSansPro',
                            color: Colors.green[700],
                            letterSpacing: 2.5,
                          ),
                        ),
                        title: Text(
                          p['Bvn'],
                          style: TextStyle(
                              fontFamily: 'BalooBhai', fontSize: 20.0),
                        ),
                      ),
                    ),
                    Card(
                      color: Colors.white,
                      margin: EdgeInsets.symmetric(
                          vertical: 10.0, horizontal: 25.0),
                      child: ListTile(
                        leading: Text(
                          'Marital Status:',
                          style: TextStyle(
                            fontSize: 20,
                            fontFamily: 'SourceSansPro',
                            color: Colors.green[700],
                            letterSpacing: 2.5,
                          ),
                        ),
                        title: Text(
                          p['MaritalStatus'],
                          style: TextStyle(
                              fontFamily: 'BalooBhai', fontSize: 20.0),
                        ),
                      ),
                    ),
                    Card(
                      color: Colors.white,
                      margin: EdgeInsets.symmetric(
                          vertical: 10.0, horizontal: 25.0),
                      child: ListTile(
                        leading: Text(
                          'Anchor:',
                          style: TextStyle(
                            fontSize: 20,
                            fontFamily: 'SourceSansPro',
                            color: Colors.green[700],
                            letterSpacing: 2.5,
                          ),
                        ),
                        title: Text(
                          p['AnchorAcronym'],
                          style: TextStyle(
                              fontFamily: 'BalooBhai', fontSize: 20.0),
                        ),
                      ),
                    ),
                    Card(
                      color: Colors.white,
                      margin: EdgeInsets.symmetric(
                          vertical: 10.0, horizontal: 25.0),
                      child: ListTile(
                        leading: Text(
                          'Gender:',
                          style: TextStyle(
                            fontSize: 20,
                            fontFamily: 'SourceSansPro',
                            color: Colors.green[700],
                            letterSpacing: 2.5,
                          ),
                        ),
                        title: Text(
                          p['Gender'],
                          style: TextStyle(
                              fontFamily: 'BalooBhai', fontSize: 20.0),
                        ),
                      ),
                    ),

                    Card(
                      color: Colors.white,
                      margin: EdgeInsets.symmetric(
                          vertical: 10.0, horizontal: 25.0),
                      child: ListTile(
                        leading: Text(
                          'Phone:',
                          style: TextStyle(
                            fontSize: 20,
                            fontFamily: 'SourceSansPro',
                            color: Colors.green[700],
                            letterSpacing: 2.5,
                          ),
                        ),
                        title: Text(
                          p['PhoneNo'],
                          style: TextStyle(
                              fontFamily: 'BalooBhai', fontSize: 20.0),
                        ),
                      ),
                    ),

                    Card(
                      color: Colors.white,
                      margin: EdgeInsets.symmetric(
                          vertical: 10.0, horizontal: 25.0),
                      child: ListTile(
                        leading: Text(
                          'State/LGA:',
                          style: TextStyle(
                            fontSize: 20,
                            fontFamily: 'SourceSansPro',
                            color: Colors.green[700],
                            letterSpacing: 2.5,
                          ),
                        ),
                        title: Text(
                          '${p['State']} / ${p['Lga']}',
                          style: TextStyle(
                              fontFamily: 'BalooBhai', fontSize: 20.0),
                        ),
                      ),
                    ),
                    Card(
                      color: Colors.white,
                      margin: EdgeInsets.symmetric(
                          vertical: 10.0, horizontal: 25.0),
                      child: ListTile(
                        leading: Text(
                          'Farm Size:',
                          style: TextStyle(
                            fontSize: 20,
                            fontFamily: 'SourceSansPro',
                            color: Colors.green[700],
                            letterSpacing: 2.5,
                          ),
                        ),
                        title: Text(
                          '${p['Size']}',
                          style: TextStyle(
                              fontFamily: 'BalooBhai', fontSize: 20.0),
                        ),
                      ),
                    ),
                    //Geo code was here

                    Divider(),

                    Row(
                      // mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        SizedBox(width: 15),
                        Icon(Icons.timelapse,
                            size: 30, color: Colors.green[400]),
                        SizedBox(width: 15),
                        Text(
                          'EOP Details',
                          style:
                              TextStyle(color: Colors.green[400], fontSize: 20),
                        ),
                      ],
                    ),

                    Padding(
                      padding: const EdgeInsets.all(5.0),
                      child: Card(
                        elevation: 4.0,
                        color: Colors.grey[100],
                        margin: EdgeInsets.only(
                            left: 10, right: 10, top: 20, bottom: 10),
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10)),
                        child: Container(
                          padding:
                              EdgeInsets.only(left: 15, top: 20, bottom: 10),
                          width: MediaQuery.of(context).size.width,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Container(width: 10),
                              Row(
                                children: <Widget>[
                                  Padding(
                                    padding: const EdgeInsets.only(
                                        left: 10, top: 10),
                                    child: Text(
                                      'TransId:',
                                      textAlign: TextAlign.left,
                                      style: TextStyle(
                                        color: Color(0xFF9b9b9b),
                                        fontSize: 14.0,
                                        decoration: TextDecoration.none,
                                        fontWeight: FontWeight.normal,
                                      ),
                                    ),
                                  ),
                                  Padding(
                                    padding: const EdgeInsets.only(
                                        left: 50, top: 12),
                                    child: Text(
                                      q['TransId'],
                                      // anchors[i]['Oid'].toString(),
                                      textAlign: TextAlign.left,
                                      style: TextStyle(
                                        color: Colors.grey[700],
                                        fontSize: 14.0,
                                        decoration: TextDecoration.none,
                                        fontWeight: FontWeight.normal,
                                      ),
                                    ),
                                  ),
                                ],
                              ),

                              Container(
                                height: 20,
                              ),
                              // button was here
                            ],
                          ),
                        ),
                      ),
                    ),
                    ListView.builder(
                        shrinkWrap: true,
                        itemCount: r.length,
                        itemBuilder: (BuildContext context, int i) {
                          ////////////// 1st card///////////

                          return Card(
                            elevation: 4.0,
                            color: Colors.grey[100],
                            margin: EdgeInsets.only(
                                left: 10, right: 10, top: 20, bottom: 10),
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10)),
                            child: SingleChildScrollView(
                              scrollDirection: Axis.vertical,
                              child: SingleChildScrollView(
                                scrollDirection: Axis.horizontal,
                                child: Container(
                                  padding: EdgeInsets.only(
                                      left: 15, top: 20, bottom: 10),
                                  width: MediaQuery.of(context).size.width,
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: <Widget>[
                                      Container(width: 10),

                                      Row(
                                        children: <Widget>[
                                          Padding(
                                            padding: const EdgeInsets.only(
                                                left: 10, top: 10),
                                            child: LimitedBox(
                                              child: Text(
                                                'Item Name:',
                                                textAlign: TextAlign.left,
                                                style: TextStyle(
                                                  color: Color(0xFF9b9b9b),
                                                  fontSize: 14.0,
                                                  decoration:
                                                      TextDecoration.none,
                                                  fontWeight: FontWeight.normal,
                                                ),
                                              ),
                                            ),
                                          ),
                                          Padding(
                                              padding: const EdgeInsets.only(
                                                  left: 20, top: 12),
                                              child: Flexible(
                                                child: Text(
                                                  r[i]['ItemName'],
                                                  textAlign: TextAlign.left,
                                                  style: TextStyle(
                                                    color: Colors.grey[700],
                                                    fontSize: 14.0,
                                                    decoration:
                                                        TextDecoration.none,
                                                    fontWeight:
                                                        FontWeight.normal,
                                                  ),
                                                ),
                                              )),
                                        ],
                                      ),

                                      Row(
                                        children: <Widget>[
                                          Padding(
                                            padding: const EdgeInsets.only(
                                                left: 10, top: 10),
                                            child: Flexible(
                                              child: Text(
                                                'Quantity Allocated:',
                                                textAlign: TextAlign.left,
                                                style: TextStyle(
                                                  color: Color(0xFF9b9b9b),
                                                  fontSize: 14.0,
                                                  decoration:
                                                      TextDecoration.none,
                                                  fontWeight: FontWeight.normal,
                                                ),
                                              ),
                                            ),
                                          ),
                                          Padding(
                                              padding: const EdgeInsets.only(
                                                  left: 20, top: 12),
                                              child: Flexible(
                                                child: Text(
                                                  '${r[i]['QuantityAllocated']}',
                                                  textAlign: TextAlign.left,
                                                  style: TextStyle(
                                                    color: Colors.grey[700],
                                                    fontSize: 14.0,
                                                    decoration:
                                                        TextDecoration.none,
                                                    fontWeight:
                                                        FontWeight.normal,
                                                  ),
                                                ),
                                              )),
                                        ],
                                      ),

                                      Container(
                                        height: 20,
                                      ),
                                      // button was here
                                    ],
                                  ),
                                ),
                              ),
                            ),
                          );
                        }),

                    Divider(),
                    Container(
                      margin: EdgeInsets.only(top: 20, bottom: 30),
                      child: Center(
                        child: RaisedButton(
                          padding: EdgeInsets.fromLTRB(80, 10, 80, 10),
                          color: Colors.green,
                          child: Text(
                            "Complete",
                            style: TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                                fontSize: 14),
                          ),
                          onPressed: () {
                            Navigator.of(context).push(MaterialPageRoute(
                                builder: (context) =>
                                    StartScanPage(widget.dc_result)));
                          },
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(50),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
    );
  }

  Future<void> get_farmer() async {
    final response = await http
        .get('http://api.ergagro.com:112/GetFarmerByBvn?Bvn=${widget.result}');
    print('${response.statusCode}yoyo');
    if (response.statusCode == 200) {
      final jsonStatus = jsonDecode(response.body);
      setState(() {
        p = jsonStatus['Farmer'];
      });
      print('${p['Oid']}');
    } else {
      throw Exception();
    }
  }

  Future<void> get_farmer_eop() async {
    SharedPreferences localStorage = await SharedPreferences.getInstance();
    var userJson = localStorage.getString('loginRes');
    user = json.decode(userJson);
    print(user['UserName']);

    final response = await http.get(
        'http://api.ergagro.com:112/GenerateFarmersEop?farmerBvn=${widget.result}&dcOid=${widget.dc_result}&agentName=${user['UserName']}',
        headers: _setHeaders());

    print('${response.statusCode}popo');
    if (response.statusCode == 200) {
      final jsonStatus = jsonDecode(response.body);
      var maineops = jsonStatus['Eop'];
      var eoplines = maineops['EopLines'];
      setState(() {
        q = maineops;
        r = eoplines;
      });
      // print('trandid');
      // print('${q['TransId']}kukuk');
    } else {
      throw Exception();
    }
  }

  _setHeaders() => {
        'Content-type': 'application/json',
        'Accept': 'application/json',
      };
}

class profile {
  String name,
      bvn,
      phone,
      appliId,
      state,
      farmsize,
      geocord,
      anchor,
      lga,
      gender,
      maritalStatus;

  profile(
      this.name,
      this.bvn,
      this.phone,
      this.appliId,
      this.state,
      this.farmsize,
      this.geocord,
      this.anchor,
      this.lga,
      this.gender,
      this.maritalStatus);
}

1 个答案:

答案 0 :(得分:0)

尝试使用r.length-1。应该会有帮助。

相关问题