我只想访问我的列表?我如何访问它并打印其中的数据。

huangapple go评论58阅读模式
英文:

I just wanna access my List ? How do I access it and print the data in it

问题

I tried `initstate` but it won't work and I don't know how to access the list I made. I just want to add all the data send to this file in `cartId` and store it in `cartid`, then I want to print it.

```dart
import 'package:flutter/material.dart';

class Cart extends StatefulWidget {
  final int cartId;
  const Cart(this.cartId, {Key? key}) : super(key: key);

  @override
  State<Cart> createState() => _CartState();
}

class _CartState extends State<Cart> {
  List<int>? cartid;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    cartid!.add(widget.cartId.toInt());
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold (
      appBar: AppBar(
        title: Text('Cart'),
      ),
      body: Text(cartid != null ? '${cartid!.length}' : "Empty"),
    );
  }
}

<details>
<summary>英文:</summary>

I tried `initstate` but it won&#39;t work and I don&#39;t know how to access the list I made. I just want to add all the data send to this file in `cartId` and store it in `cartid` , then I want to print it.

import 'package:flutter/material.dart';

class Cart extends StatefulWidget {
final int cartId ;
const Cart(this.cartId,{Key? key}) : super(key: key);

@override
State<Cart> createState() => _CartState();
}

class _CartState extends State<Cart> {
List<int>? cartid;
@override
void initState() {
// TODO: implement initState
super.initState();
cartid!.add(widget.cartId.toInt());
}
@override
Widget build(BuildContext context) {
return Scaffold (
appBar: AppBar(
title: Text('Cart'),
),
body: Text(cartid != null ? '${cartid!.length}' : "Empty"),
);
}
}


</details>


# 答案1
**得分**: 1

```dart
您正在尝试访问您未在任何地方创建的列表。尝试这样做:

```dart
class Cart extends StatefulWidget {
  final int cartId;
  const Cart(this.cartId, {super.key});

  @override
  State<Cart> createState() => _CartState();
}

class _CartState extends State<Cart> {
  List<int>? cartid;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    cartid = [widget.cartId];
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Cart'),
      ),
      body: Text(cartid != null ? '${cartid!.length}' : "Empty"),
    );
  }
}

<details>
<summary>英文:</summary>

You&#39;re trying to access the list that you didn&#39;t create anywhere. Try to do this:

```dart
class Cart extends StatefulWidget {
  final int cartId;
  const Cart(this.cartId,{super.key});

  @override
  State&lt;Cart&gt; createState() =&gt; _CartState();
}

class _CartState extends State&lt;Cart&gt; {
  List&lt;int&gt;? cartid;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    cartid = [widget.cartId];
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold (
      appBar: AppBar(
        title: const Text(&#39;Cart&#39;),
      ),
      body: Text(cartid != null ? &#39;${cartid!.length}&#39; : &quot;Empty&quot;),
    );
  }
}

In the example above, we're instantiating the list in the [widget.cartId]. There are many ways to do that, tho.

huangapple
  • 本文由 发表于 2023年3月9日 21:47:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定