如何使用小部件的值从有状态到无状态 [英] how to use widget value from stateful into stateless

查看:55
本文介绍了如何使用小部件的值从有状态到无状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直坚持使用从第一页传递的值将值从第二页传递到第三页.当我尝试使用有状态窗口小部件中调用数据的常用方法时,它会在未定义时返回错误.

I am having a stuck on transferring the value from the second page to the third page using the value pass from the first page. when I am trying to use the usual way that I am used to calling the data in the stateful widget, it returns an error on undefined.

class restaurantLISTVIEW extends StatefulWidget {

  restaurantLISTVIEW({this.category});
  final String category;

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

在扩展状态内:(工作中)

within extends state:(working)

FloatingActionButton(
          onPressed: (){
            return showDialog(
              context: context,
              builder: (context){
                return AlertDialog(
                  content: Text(
                      '${widget.category}'
                  ),
                );
              },
            );
          },),

扩展无状态小部件时出错,返回null :(不起作用)

error on extends stateless widget, return null: (not working)

onTap: ()
            {
              Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context)=> new SarapanPagi(list: list, index: i, category: category)));
            }

内部扩展为无状态:(不起作用)

within extends stateless:(not working)

FloatingActionButton(
          onPressed: (){
            return showDialog(
              context: context,
              builder: (context){
                return AlertDialog(
                  content: Text(
                      '$category hellp'
                  ),
                );
              },
            );
          },),

第二页上的整个代码:

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as HTTP;
import 'breakfast.dart';

// ignore: camel_case_types
class restaurantLISTVIEW extends StatefulWidget {

  restaurantLISTVIEW({this.category});
  final String category;

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

// ignore: camel_case_types
class _restaurantLISTVIEWState extends State<restaurantLISTVIEW> {


  Future<List> getData() async{

var url = 'http://10.0.2.2/foodsystem/restaurantlist.php';
var data = {'product_type': 'xde pape ppon saje nk hantar value'};
var response = await http.post(url, body: json.encode(data));
//final response= await http.get("http://10.0.2.2/foodsystem/getdata.php");
return json.decode(response.body);}

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: IconThemeData(color: Colors.black),
    title: Row(
      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
      children: [
        //Text("Restaurant's Owner Page"),
        Text('${widget.category}', textAlign: TextAlign.center, style: TextStyle(fontSize: 25, fontWeight: FontWeight.w700), ),
      ],
    ),
    centerTitle: false,
    //automaticallyImplyLeading: false,
  ),
  body:
  Padding(
    padding: const EdgeInsets.only(bottom: 20, left: 5, right: 5),
    child: Column(
      children: [
        SizedBox(height: 30,),
        Container(
          //decoration: BoxDecoration(border: Border.all(color: Colors.black, width: 4), borderRadius: BorderRadius.circular(15)),
          height: 600,
          child: FutureBuilder<List>(
            future: getData(),
            builder: (context, snapshot){
              if(snapshot.hasError) print(snapshot.error);

              return snapshot.hasData ?
              ItemList(list: snapshot.data,) :
              Center(child: CircularProgressIndicator(),);
            },
          ),
        ),
        FloatingActionButton(
          onPressed: (){
            return showDialog(
              context: context,
              builder: (context){
                return AlertDialog(
                  content: Text(
                      '${widget.category}'
                  ),
                );
              },
            );
          },
        ),
        SizedBox(height: 10,),
      ],
    ),
  ),
);
  }
}
class ItemList extends StatelessWidget {

  final List list;
  final String category;
  ItemList({this.list, this.category});
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        //color: Colors.red.shade100,
        height: 600,
        child: ListView.builder(
          itemCount: list==null ? 0 : list.length,
          itemBuilder: (context, i){
            return new Container(
              height: 200,
              child: new GestureDetector(
                onTap: ()
            {
              if(widget.category == "Breakfast"){
                Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context)=> new SarapanPagi(list: list, index: i, category: category)));
              }
            },
            child: new Card(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Container(
                        constraints: BoxConstraints(minWidth: 180, maxWidth: 180),
                        child:
                        Column(
                          children: [
                            Text(list[i]["restaurant_name"], style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold), textAlign: TextAlign.center,),
                            Text("Restaurant ID: ${list[i]["restaurant_id"]}", style: TextStyle(fontSize: 20,), textAlign: TextAlign.center,),
                          ],
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(left :20.0),
                        child: Container(
                          constraints: BoxConstraints(minWidth: 150, maxWidth: 300),
                          child:
                          SizedBox(
                            width: 50,
                            child: Column(
                              children: [
                                Text("SSM: ${list[i]["restaurant_ssm"]}", textAlign: TextAlign.center, style: TextStyle(fontSize: 15),),
                                Text("Phone: ${list[i]["restaurant_phone"]}", textAlign: TextAlign.center, style: TextStyle(fontSize: 15),),
                                Text("Address: ${list[i]["restaurant_address"]}", textAlign: TextAlign.center, style: TextStyle(fontSize: 15),),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: [

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

第三页的完整代码:

import 'dart:convert';
import 'package:http/http.dart' as HTTP;
import 'package:flutter/material.dart';
//import 'globals.dart' as globals;

class SarapanPagi extends StatefulWidget {
  final List list;
  final int index;
  final String category;
  SarapanPagi({this.index,this.list,this.category});


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

class _SarapanPagiState extends State<SarapanPagi> {

  Future<List> getData() async{

    var url = 'http://10.0.2.2/foodsystem/breakfastlist.php';
    var data = {
      'product_type': 'Breakfast',
      'product_owner': widget.list[widget.index]['restaurant_id'],
    };
    var response = await http.post(url, body: json.encode(data));
    //final response= await http.get("http://10.0.2.2/foodsystem/getdata.php");
    return json.decode(response.body);}

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: IconThemeData(color: Colors.black),
    title: Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        //Text("Restaurant's Owner Page"),
        Text(widget.list[widget.index]['restaurant_name'], textAlign: TextAlign.center, style: TextStyle(fontWeight: FontWeight.w700), ),
      ],
    ),
    centerTitle: false,
    //automaticallyImplyLeading: false,
  ),
  body:
  Padding(
    padding: const EdgeInsets.only(bottom: 20, left: 5, right: 5),
    child: Column(
      children: [
        SizedBox(height: 30,),
        Container(
          //decoration: BoxDecoration(border: Border.all(color: Colors.black, width: 4), borderRadius: BorderRadius.circular(15)),
          height: 600,
          child: FutureBuilder<List>(
            future: getData(),
            builder: (context, snapshot){
              if(snapshot.hasError) print(snapshot.error);

              return snapshot.hasData ?
              ItemList(list: snapshot.data,) :
              Center(child: CircularProgressIndicator(),);
            },
          ),
        ),
        SizedBox(height: 10,),
      ],
    ),
  ),
);
  }
}
class ItemList extends StatelessWidget {

  final List list;
  ItemList({this.list});
  @override
  Widget build(BuildContext context) {
return SingleChildScrollView(
  child: Container(
    //color: Colors.red.shade100,
    height: 600,
    child: ListView.builder(
      itemCount: list==null ? 0 : list.length,
      itemBuilder: (context, i){
        return new Container(
          height: 200,
          child: new GestureDetector(
            onTap: (){},
            child: new Card(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Image.asset(
                          "images/${list[i]['image']}",
                          width: 150,
                          height: 150,
                        ),
                      ),
                      Padding(
                        padding: const EdgeInsets.only(left: 20.0, bottom: 0),
                        child:
                        Column(
                          children: [
                            Text(list[i]["product_name"], textAlign: TextAlign.center, style: TextStyle(fontSize: 30),),
                            Text("Price RM : ${list[i]["product_price"]}", textAlign: TextAlign.center, style: TextStyle(fontSize: 25),),
                            RaisedButton(
                                shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(40.0)),
                                color: Colors.red.shade300,
                                child: Text("Add to Cart", style: TextStyle(fontWeight: FontWeight.bold),),
                                onPressed: (){},
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                  /*ListTile(
                    title: Text(list[i]["product_name"], textAlign: TextAlign.center, style: TextStyle(fontSize: 30),),
                    leading:
                    Image.asset(
                      "images/${list[i]['image']}",
                      width: 100,
                      height: 100,
                    ),
                    subtitle: Text("Price RM : ${list[i]["product_price"]}", textAlign: TextAlign.center, style: TextStyle(fontSize: 25),),
                  ),*/
                ],
              ),
            ),
          ),
        );
      },
    ),
  ),
);

The

推荐答案

您没有在 restaurantLISTVIEW 页面中使用 widget.category ,而是在 ItemList 中使用了>小部件,这就是您收到该错误的原因.像这样调整您的 ItemList 小部件

You are not using widget.category in restaurantLISTVIEW page but in ItemList widget, that's why you are getting that error.
Adjust your ItemList widget like this

class ItemList extends StatelessWidget {
  final List list;
  final String category;

  ItemList({this.list, this.category});
}

因此,您在 ItemList 中的导航行必须为

So, your navigation line in ItemList must be

Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context)=> new SarapanPagi(list: list, index: i, category: category)));

此外,在 SarapanPagi 页面中,类别的变量类型不是 int String

Also, in SarapanPagi page the variable type of category is not int by String

这篇关于如何使用小部件的值从有状态到无状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆