Flutter: i get invalid argument(s) Error in flutter if i want to navigate to detail page

1

I want to navigate to detail page. But i get invalid argument error. In detail page, i try to get detail information from my online server. If i click on Hot reload button in android studio, the error disappear. Please, How can i do to fix this error ? Screenshoot of error

the error log :

════════ (2) Exception caught by widgets library ═══════════════════════════════════════════════════
Invalid argument(s): The source must not be null
The relevant error-causing widget was: 
  DetailArticlePage file:///C:/Users/abiboo/FlutterProject/projectname/lib/miledoo_widget/home.dart:515:39
//home.dart
return new Container(
          margin: EdgeInsets.symmetric(vertical: 8.0),
          height: 240.0,
          child: ListView.builder(
              scrollDirection: Axis.horizontal,
              itemCount: articledata.length,
              itemBuilder: (BuildContext context,int index) {
                return Padding(
                  padding: EdgeInsets.all(4),
                  child: InkWell(
                    onTap: (){
                      Navigator.push(
                          context,
                          new MaterialPageRoute(
                              builder: (context) =>
                                  DetailArticlePage(int.parse(articledata[index].idArt), articledata[index].designation)));
                    },
                    child: Container(
                      decoration: BoxDecoration(
                        boxShadow: [
                          BoxShadow(

This is a piece of code for detail page who contain a method to get detail informaation from server

//detailpage.dart
import 'package:cached_network_image/cached_network_image.dart';
import 'package:f_miledoo/miledoo_widget/detail_boutique_page.dart';
import 'package:f_miledoo/miledoo_widget/panier_page.dart';
import 'package:f_miledoo/models/detail_article_models.dart';
import 'package:f_miledoo/models/panier_models.dart';
import 'package:f_miledoo/shared/constants.dart';
import 'package:f_miledoo/utils/database_helper.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../localisation_internationnalisation/localisation.dart';
import 'dart:async';
import 'package:http/http.dart' as http;
import 'dart:convert';

class DetailArticlePage extends StatefulWidget{
  final int id_art;
  final String art_desgnation;
  DetailArticlePage(this.id_art, this.art_desgnation);

  DetailArticlePages createState() => DetailArticlePages();
}

class DetailArticlePages extends State<DetailArticlePage> {

  DatabaseHelper helper = DatabaseHelper();
  PanierModel _panier = new PanierModel.withempty();

  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();



  int qtecompter = 1;
  double prixTotal = 0.0;
  String aAfficher = "";

  int taillePanier = 0;


  String idArt;
  String designation;
  String descrip;
  String prixUnit;
  String prixUnit2;
  String qteStock;
  String idBou;
  String lienPhoto;
  String nomBou;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    getpanierTaille();
    this.getRecapArticleData(widget.id_art, widget.art_desgnation);

  }

  getpanierTaille() async => taillePanier = await helper.getCount();





  getRecapArticleData(int id_article, String nom_article) async {
    final response = await http.get(BASE + "xxxxxxx?id_art="+ id_article.toString() +"&nom_art="+ nom_article);
    if (response.statusCode == 200) {
      final jsonResponse = json.decode(response.body);

      TheDetailData myData = new TheDetailData.fromJson(jsonResponse);
      for (var i = 0; i < myData.articledetail.list_recap_article.length; i++) {
        idArt = myData.articledetail.list_recap_article[i].idArt;
        designation = myData.articledetail.list_recap_article[i].designation;
        descrip = myData.articledetail.list_recap_article[i].descrip;
        prixUnit = myData.articledetail.list_recap_article[i].prixUnit;
        prixUnit2 = myData.articledetail.list_recap_article[i].prixUnit2;
        qteStock = myData.articledetail.list_recap_article[i].qteStock;
        lienPhoto = myData.articledetail.list_recap_article[i].lienPhoto;
        idBou = myData.articledetail.list_recap_article[i].idBou;

        print("id_art = " + idArt + " designation = " + designation +
            "  prixUnit2 = " + prixUnit2 + "  Qté = " + qteStock);
      }

    } else {
      throw Exception("Failed to load Data");
    }
  }

  Future<List<ListArtMemeCate>> _getSameArticleData(int id_article, String nom_article) async {
    final response = await http.get(BASE + "xxxxxxx?id_art="+ id_article.toString() +"&nom_art="+ nom_article);
    if (response.statusCode == 200) {
      final jsonResponse = json.decode(response.body);

      TheDetailData myData = new TheDetailData.fromJson(jsonResponse);
      List<ListArtMemeCate> datas = [];

      for (var i = 0; i < myData.articledetail.list_art_meme_cate .length; i++) {
        datas.add(myData.articledetail.list_art_meme_cate[i]);
      }
      return datas;
    } else {
      throw Exception("Failed to load Data");
    }
  }

  @override
  Widget build(BuildContext context) {

    int localStockQte = int.parse(qteStock);
    double localPrixUnit2 = double.parse(prixUnit2.toString());



    Widget article_afficher333 = FutureBuilder(
      future: _getSameArticleData(widget.id_art, widget.art_desgnation),
      builder: (context, snapshot) {
        //if(snapshot.data != null){
        if (snapshot.hasData) {
          List<ListArtMemeCate> articledata = snapshot.data;
          return new Container(
            child: GridView.count(
              shrinkWrap: true,
              crossAxisCount: 2,
              childAspectRatio: 0.7,
              padding: EdgeInsets.only(top: 8, left: 6, right: 6, bottom: 12),
              children: List.generate(articledata.length, (index){
                return Container(
                  child: Card(
                    clipBehavior: Clip.antiAlias,
                    child: InkWell(
                      onTap: () {
                            Navigator.push(
                            context,
                                new MaterialPageRoute(
                                    builder: (context) =>
                                    DetailArticlePage(int.parse(articledata[index].idArt) , articledata[index].designation)));
                      },
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          SizedBox(
                            height: (MediaQuery.of(context).size.width / 2 - 40),
                            width: double.infinity,
                            child: CachedNetworkImage(
                              fit: BoxFit.cover,
                              imageUrl: BASEIMAGES+articledata[index].lienPhoto,
                              placeholder: (context, url) => Center(
                                  child: CircularProgressIndicator()
                              ),
                              errorWidget: (context, url, error) => new Icon(Icons.image),
                            ),
                          ),
                          Padding(
                            padding: const EdgeInsets.only(top: 2.0),
                            child: ListTile(
                              title: Text((() {
                                if(articledata[index].designation.length >12){
                                  return "${articledata[index].designation.substring(0, 12)}...";}

                                return "${articledata[index].designation}";
                              })(), style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15.0, fontFamily: "Questrial")), 
                              subtitle: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Row(
                                    children: <Widget>[
                                      Padding(
                                        padding: const EdgeInsets.only(top: 2.0, bottom: 1),
                                        child: Text(articledata[index].prixUnit2+" F CFA", style: TextStyle(
                                            color: Theme.of(context).accentColor,
                                            fontWeight: FontWeight.w700,
                                            fontFamily: 'Questrial'
                                        )),
                                      ),
                                    ],
                                  ),

                                ],
                              ),
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                );
              }),

            ),
          );
        } else if (snapshot.hasError) {
          return Container(
            child: Center(
              child: Text(
                  "Erreur de chargement. Verifier votre connexion internet"),
            ),
          );
        }
        return new Center(
          child: CircularProgressIndicator(),
        );
      },
    );


    setState(() {
      _panier.nom_article = designation;
      _panier.prixUnit2 = double.parse(prixUnit2);
      _panier.image_article = lienPhoto;
      _panier.prixTot = qtecompter*double.parse(prixUnit2);
      _panier.quantite = qtecompter ;
      _panier.id_article = int.parse(idArt);
    });

    void _incrementeQte(){
      setState((){
        if(qtecompter >= localStockQte){
          qtecompter = localStockQte;
        }
        qtecompter++;
      });
    }
    void _decrementeQte(){
      setState((){
        if(qtecompter <=1){
          qtecompter = 1;
        }else{
          qtecompter--;
        }
      });
    }

    String _getPrixTotal(){
      setState((){
        prixTotal = qtecompter*localPrixUnit2;
            aAfficher = prixTotal.toString();
      });
      return aAfficher;
    }
void _ajouterPanier() async {
      //s.addToCart(widget.article);
      var result = await helper.addCart(_panier);
      var result3 = await helper.getCount();
      /*int result;
      result = await helper.addCart(_panier);
      if(result != 0)
        print('STATUS Panier Save Successfully');*/
    }


    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text("designation", style:  TextStyle(color: Colors.white),),
        iconTheme: new IconThemeData(color: Colors.white),
        actions: <Widget>[

          Stack(
            children: <Widget>[
              IconButton(
                icon: Icon(Icons.shopping_cart, color: Colors.white, ),
                onPressed: () {
                  //showAlertDialog(context);
                  Navigator.push(context, new MaterialPageRoute(builder: (context) => PanierPage()));
                },
              ),
              Container(
                  width: 25,
                  height: 25,
                  decoration: BoxDecoration(
                    color: Colors.red,
                    borderRadius: BorderRadius.circular(30),
                  ),
                  alignment: Alignment.center,
                  child: Text(taillePanier.toString(),
                    style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: 12
                    ),)
              ),
            ],
          ),
        ],
      ),
      body: new ListView(
        children: <Widget>[
          new Container(
            height: 240,
            child: new Hero(tag: lienPhoto, child: new Material(
              child: InkWell(
                child: new Image.network(
                    BASEIMAGES + lienPhoto,
                    fit: BoxFit.cover),
              ),
            )),
          ),
          new Container(
            child: new Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.only(top: 10,left: 5),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      new Text(designation.toString(), style: new TextStyle(fontSize: 20.0, color: Colors.black, fontFamily: 'Questrial'),),
                      ],
                  ),
                ),
                SizedBox(height: 10.0),
                Padding(
                  padding: const EdgeInsets.only(left: 280),
                  child: new Text(prixUnit2.toString()+" F CFA", style: new TextStyle(fontSize: 15.0, color: Colors.black54, fontFamily: 'Questrial', fontWeight: FontWeight.bold),),
                ),

                SizedBox(height: 10.0),
                Padding(
                  padding: const EdgeInsets.only(left: 20, right: 20, bottom: 10),
                  child: new Text(descrip.toString(), style: new TextStyle(fontSize: 15.0, color: Colors.grey, fontFamily: 'Questrial'),),
                ),
                SizedBox(height: 10.0),
                new Container(
                  height: 100,

              child: Column(
                children: <Widget>[
                  Container(
                    height: 1.0,
                    color: Colors.grey,
                  ),
                  Padding(
                    padding: const EdgeInsets.only(left: 25, right: 25, top: 20),
                    child: new Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(AppLocalizations.of(context)
                        .translate('_QUANTITY'), style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15, fontFamily: 'Questrial'),),
                        Text(AppLocalizations.of(context)
                            .translate('_TOTAL_PRICE'), style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15, fontFamily: 'Questrial'), ),
                      ],
                    ),
                  ),

                  Padding(
                    padding: const EdgeInsets.only(left: 10, right: 25),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        new Row(
                          children: <Widget>[
                            new IconButton(
                              icon: Icon(Icons.remove_circle_outline, color: Colors.amber, ),
                              onPressed: _decrementeQte,
                            ),
                            new Text("$qtecompter"),
                            new IconButton(
                              icon: Icon(Icons.add_circle_outline, color: Colors.amber, ),
                              onPressed: _incrementeQte,
                            ),
                          ],
                        ),
                        new Text(_getPrixTotal()+" F CFA", style: new TextStyle(fontSize: 20.0, color: Colors.grey),),
                      ],
                    ),
                  ),
                  Container(
                    height: 1.0,
                    color: Colors.grey,
                  ),
                ],
              ),
            ),
            //articleMemeCategorie()
          ],
        ),
      ),
      Padding(
        padding: const EdgeInsets.only(top: 10, left: 25, ),
        child: Text(AppLocalizations.of(context)
            .translate('_ITEMS_OF_SAME_QUATEGORY'), style: TextStyle(fontFamily: 'Questrial', fontSize: 15, fontWeight: FontWeight.bold),),
      ),
      article_afficher333,
    ],
  ),
    bottomNavigationBar: new Container(
      color: Colors.white,
      child: new Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Expanded(child: new MaterialButton(
            onPressed: (){showAlertDialog(context);},
            child: new Row(
              //crossAxisAlignment: CrossAxisAlignment.end,
              children: <Widget>[
                IconButton(
            //widget.article
                  icon: Icon(Icons.store, color: Colors.white,),
                  onPressed: () {
                    //showAlertDialog(context);
                    Navigator.push(
                        context,
                        new MaterialPageRoute(
                            builder: (context) =>
                                DetailBoutiquePage(int.parse(idBou)))); },
                ),
              ],
            ),
            color: Color(0xFFFFC23A),
          ),),

          Expanded(
            flex: 2,
            child: new MaterialButton(
            onPressed: (){
              _ajouterPanier();
            },
            child: new Container(
              child: new Row(
                children: <Widget>[
                  IconButton(
                    icon: Icon(Icons.add_shopping_cart, color: Colors.white,),
                  ),
                  Text(AppLocalizations.of(context)
    .translate('_ADD_TO_CART'), style: TextStyle(color: Color(0xFFFFFFFF)))
                ],
              ),
            ),
            color: Color(0xFFFFC23A),
          ),),

          Expanded(child: new MaterialButton(
            onPressed: (){showAlertDialog(context);},
            child: new Text(AppLocalizations.of(context)
                .translate('_BUY_NOW'), style: TextStyle(color: Color(0xFFFFFFFF)),),
            color: Color(0xFFFFC23A),
          ),),
        ],
      ),
    )

);
  }

}
android
android-studio
http
flutter
dart
asked on Stack Overflow May 11, 2020 by alabiboo • edited May 15, 2020 by Crazy Lazy Cat

1 Answer

1

Without seeing your full detail page and error log it is difficult to tell exactly what your error is.

From the information you have given it sounds like you are trying to retrieve data from an Http request, and then use that information for the display of your page. Have you tried using a FutureBuilder in the detail page? This will allow you to display a page while waiting for the data from the server, and will then display the data from the server once it has been retrieved.

answered on Stack Overflow May 11, 2020 by Philip Marsh

User contributions licensed under CC BY-SA 3.0