简体   繁体   English

在我的 Flutter 上,我有一个数据列表,我想将此数据发送到另一个屏幕,但是有一个错误,我不明白如何解决它

[英]On my Flutter I have a list of data and I want to send this data to another screen but there is an error that I don't understand how to solve it

I want to use navigator to pass data but i can't solve the error I actually don't know how to pass data between screens properly using the navigator in cases like this I've learned but I still don't understand how because every tutorial I watch and other ways from the internet each have a different way我想使用导航器传递数据但我无法解决错误我实际上不知道如何在这样的情况下使用导航器在屏幕之间正确传递数据我已经学会了但我仍然不明白如何因为每个我观看的教程和互联网上的其他方式各有不同的方式

This is the image这是图像

List of data数据列表

Navigator航海家

2nd Screen第二屏

class for list of data class 用于数据列表

here's the error:这是错误:

这是错误

below is the full code list of data and navigator下面是数据和导航器的完整代码列表

import 'package:semester3_prjct/test.dart';
import 'data.dart';
import 'package:semester3_prjct/slide.dart';
import 'package:semester3_prjct/navBar.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'ProJect',
      theme: ThemeData(primaryColor: Color(0xFF9FA8DA)),
      home: Home(),
    );
  }
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  final List<Isi> isi = [
    Isi(
        text: 'Makanan 1',
        gbr: 'images/gbr1.jpg',
        harga: 'Harga: Rp.6000/porsi'),
    Isi(
        text: 'Makanan 2',
        gbr: 'images/gbr2.jpg',
        harga: 'Harga: Rp.6000/kotak'),
    Isi(
        text: 'Makanan 3',
        gbr: 'images/gbr3.jpg',
        harga: 'Harga: Rp.7000/porsi'),
    Isi(
        text: 'Makanan 4',
        gbr: 'images/gbr4.jpg',
        harga: 'Harga: Rp.8000/porsi'),
    Isi(text: 'Makanan 5', gbr: 'images/gbr5.jpg', harga: 'Harga: Rp.5000/ptg'),
    Isi(
        text: 'Makanan 6',
        gbr: 'images/gbr6.jpg',
        harga: 'Harga: Rp.7000/kotak'),
    Isi(
        text: 'Makanan 7',
        gbr: 'images/gbr7.jpg',
        harga: 'Harga: Rp.8000/porsi'),
    Isi(
        text: 'Makanan 8',
        gbr: 'images/gbr8.jpg',
        harga: 'Harga: Rp.12000/porsi'),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      drawer: Navbar(),
      appBar: AppBar(
        backgroundColor: Colors.indigo[200],
        elevation: 0.0,
        flexibleSpace: Slideku(),
        toolbarHeight: 137,
        leading: Align(
          alignment: Alignment.topLeft,
          child: Builder(
            builder: (BuildContext context) {
              return IconButton(
                icon: const Icon(Icons.menu),
                onPressed: () {
                  Scaffold.of(context).openDrawer();
                },
                tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
              );
            },
          ),
        ),
        actions: <Widget>[
          Align(
              alignment: Alignment.topRight,
              child: IconButton(
                onPressed: () {},
                icon: Icon(Icons.search, color: Colors.white),
              )),
        ],
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Container(
              height: 10,
              decoration: BoxDecoration(
                  color: Colors.indigo[200],
                  borderRadius: BorderRadius.only(
                      ))),
          Expanded(
            child: GridView.count(
              crossAxisCount: 2,
              padding: EdgeInsets.all(10.0),
              childAspectRatio: (4 / 4.8),
              crossAxisSpacing: 5,
              mainAxisSpacing: 5,
              children: isi
                  .map((item) => Card(
                      elevation: 10.0,
                      shadowColor: Colors.indigo,
                      color: Colors.grey[100],
                      shape: RoundedRectangleBorder(
                        side:
                            BorderSide(color: Colors.indigo.shade200, width: 2),
                        borderRadius: BorderRadius.circular(15.0),
                      ),
                      margin: EdgeInsets.fromLTRB(5.0, 10.0, 5.0, 0),
                      child: InkWell(
                          onTap: () {
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => Test(isi: isi)));
                          },
                          child: Padding(
                              padding: const EdgeInsets.all(10),
                              child: Column(
                                children: <Widget>[
                                  Image.asset(item.gbr,
                                      fit: BoxFit.fitWidth, height: 125),
                                  Container(
                                    height: 10,
                                  ),
                                  Text(
                                    item.text,
                                    style: TextStyle(
                                        fontSize: 14,
                                        fontWeight: FontWeight.bold,
                                        color: Colors.grey[700]),
                                  ),
                                  Container(
                                    height: 4,
                                  ),
                                  Text(
                                    item.harga,
                                    style: TextStyle(
                                        fontSize: 11, color: Colors.grey[700]),
                                  ),
                                ],
                              )))))
                  .toList(),
            ),
          )
        ],
      ),
    );
  }
}```

On the following code:在以下代码上:

onTap: () {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => Test(isi: isi)));
  },

you're giving variable isi which is a list:你给变量isi这是一个列表:

 final List<Isi> isi

But your Test widget need an Isi object:但是您的Test小部件需要一个Isi object:

class Test extends StatelesWidget {
    final Isi isi;

    ....

}

To solve the problem, use the item from your map:要解决此问题,请使用 map 中的item

isi.map((item) => Card( ...

So, your code should be something like this:所以,你的代码应该是这样的:

onTap: () {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => Test(isi: item)));
  },

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不明白如何使用列表修复错误 - I don't understand how to fix the error with a list 我想在数据到达服务器时动态更改小部件颜色,但我不明白 - I want to change the widget color dynamically when data will come server but i don't understand 我不明白如何从 dart 中的 API 获取数据 - I don't understand how to fetch data from API in dart 我的 Flutter Android 文件夹显示错误,我不明白错误在哪里 - My Flutter Android folder shows an error and I don't understand where the error is 我想在颤振中将数据从一个屏幕传递到另一个屏幕并将数据保存到 ListView/ListTiles - I want to pass data from one screen to another screen in flutter and save the data to ListView/ListTiles 我有一个未来<list>从 flutter 中的 sqlite 返回数据。 我想将此列表转换为 ListView。 我怎样才能做到这一点</list> - I have a Future<List> that returns data from sqlite in flutter. I want to convert this list into ListView. How can I do that 在颤动中将数据发送到新屏幕,但不知道如何设置 - send data to new screen in flutter but don't know how to set this 如何通过构造函数将数据传递到 flutter 中的卡列表? 我已经尝试了这些东西,结果和想要的东西都附上了 - How to Pass Data to Card list in flutter through constructor ? I have Tried these things, result and want is attached 我想知道如何解决“颤动”的“列表”错误 - I wonder how to solve the 'List' error of 'flutter' 我有一个存储的列表数据,如何在 flutter 中对其进行本地化? - I have a stored List data and how can I localize it in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM