Flutter Firebase RTDB检索特定孩子的问题

时间:2020-03-17 06:43:12

标签: flutter firebase-realtime-database

我尝试从Firebase RTDB的特定子项检索数据。问题是出现以下错误(我不使用int):

Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'

日志显示错误出现在这里

  DATA[individualKey]['name'],
            DATA[individualKey]['picture'],
            DATA[individualKey]['price'],
          DATA[individualKey]['id'],
          DATA[individualKey]['brand'],
          DATA[individualKey]['category'],
//          DATA[individualKey]['feature'],
//          DATA[individualKey]['sale'],
//          DATA[individualKey]['colors'],
//          DATA[individualKey]['sizes'],
          DATA[individualKey]['quantity'],

我将要显示的代码,我在最小更改的情况下在另一个屏幕上使用了它,并且可以正常工作 但在我目前使用的那套软件中,它不起作用

class PostsDetails extends StatefulWidget {

  final value2;
  PostsDetails({Key key,@required this.value2}) : super(key : key);

  @override
  _PostsDetailsState createState() => _PostsDetailsState(value2);
}

class _PostsDetailsState extends State<PostsDetails> {

  final value2;
  _PostsDetailsState(this.value2);

  List<Posts>postsList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    DatabaseReference postsRef = FirebaseDatabase.instance.reference().child("Product").child("Shoes").child("Nike").child(value2);
    postsRef.once().then((DataSnapshot snap)
    {
      var KEYS = snap.value.keys;
      var DATA = snap.value;

      postsList.clear();

      for(var individualKey in KEYS)
      {
        Posts posts = new Posts
          (

          DATA[individualKey]['name'],
          DATA[individualKey]['picture'],
          DATA[individualKey]['price'],
          DATA[individualKey]['id'],
          DATA[individualKey]['brand'],
          DATA[individualKey]['category'],
//          DATA[individualKey]['feature'],
//          DATA[individualKey]['sale'],
//          DATA[individualKey]['colors'],
//          DATA[individualKey]['sizes'],
          DATA[individualKey]['quantity'],



        );

        postsList.add(posts);

      }

    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.1,
        backgroundColor: Colors.red,
        title: InkWell(
            onTap: (){
              Navigator.push(context, MaterialPageRoute(builder: (context)=> new HomePage()));
            },
            child: Text("SHOP APP")),
        actions: <Widget>[
          new IconButton(icon: Icon(Icons.search, color: Colors.white,), onPressed: (){}),

        ],
      ),
       body: new ListView(
        children: <Widget>[
        new  Container(

          child: postsList.length == 0 ? new Text("No Available"):new ListView.builder(
          scrollDirection: Axis.vertical,
          shrinkWrap: true,
          itemCount: postsList.length,
          itemBuilder: (_, index){


                new Container(
                    height: 300.0,
                    child: GridTile(
                    child: Container(
                    color: Colors.white,
//                    child: Image.network(postsList[index].picture),
                ),
                footer: new Container(
                color: Colors.white70,
                child: ListTile(
                leading: new Text(postsList[index].name,
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16.0),),
                title: new Row(
                children: <Widget>[
                Expanded(
//                child: new Text("\$"+postsList[index].price, style: TextStyle(fontWeight: FontWeight.bold, color: Colors.red),)
                ),
                ],
                ),
                ),
                ),
                ),
                );




          })

          ),
        ]
        )

    );
  }
}

postslist.length检索0。

最终值2是其子ID。我已经确认从上一个屏幕收到的好评 如果未使用value2,将无法检索数据

帖子类别:

class Posts
{
  String name;
  String picture;
  String price;
  String id;
  String brand;
  String category;
  String quantity;
//  List<> colors;
//  List<> sizes;
//  bool feature;
//  bool sale;

  Posts(this.name, this.picture, this.price, this.id,  this.category,this.brand,this.quantity);
}

Firebase RTDB

如何解决该问题,或者有某种方法可以找回一个特定的孩子?我知道如何显示列表,但不显示特定孩子的感谢

1 个答案:

答案 0 :(得分:0)

我由于某种原因修复了代码

postsList.length == 0 ? new Text("No Available"):

是问题,所以我删除了