flutter 连接到待更新的Firebase数据时状态不佳

ahy6op9u  于 2023-01-21  发布在  Flutter
关注(0)|答案(1)|浏览(109)

所以,我想做一个flutter页面,它从firebase的一个集合中获取数据,并根据标题显示条目,当我按下一个标题时,它会显示条目的完整数据。然后,在详细信息页面中,在数据后面做一个大的文本框,用户可以引入数据,以及一个提交按钮。文本框中的数据会更新表中的响应字段。
这是我的代码,我的工作。请注意,我是一个初学者,我可能会犯一个新手的错误在这里和那里:

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

class ResponsePage extends StatefulWidget {
  @override
  _ResponsePageState createState() => _ResponsePageState();
}

class _ResponsePageState extends State<ResponsePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xFF006400),
        title: Text('Visualize Personal'),
      ),
      body: StreamBuilder(
        stream: FirebaseFirestore.instance.collection('personal').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }

          return ListView.builder(
            itemCount: snapshot.data!.docs.length,
            itemBuilder: (context, index) {
              final form = snapshot.data!.docs[index];

              return ListTile(
                title: Text(form['title']),
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => DetailPage(form: form),
                    ),
                  );
                },
              );
            },
          );
        },
      ),
    );
  }
}

class DetailPage extends StatefulWidget {
  DetailPage({Key? key, required this.form}) : super(key: key);

  final DocumentSnapshot form;
  @override
  _DetailPageState createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  final TextEditingController _responseController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Detail Page')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // Display the data for the entry
            Text('Title: ${widget.form['title']}'),
            Text('Description: ${widget.form['description']}'),
            Text('Name: ${widget.form['name']}'),
            Text('Number: ${widget.form['number']}'),
            Text('Response: ${widget.form['response']}'),
            Text('Street: ${widget.form['street']}'),
            Text('Type: ${widget.form['type']}'),
            SizedBox(height: 16.0),
            // Add the text field and submit button
            TextField(
              controller: _responseController,
              decoration: InputDecoration(
                labelText: 'Response',
                border: OutlineInputBorder(),
              ),
            ),
            SizedBox(height: 8.0),
            ElevatedButton(
              child: Text('Submit'),
              onPressed: () {
                // Update the response field in the Firebase collection
                FirebaseFirestore.instance
                    .collection('your_collection')
                    .doc(widget.form.id)
                    .update({
                  'response': _responseController.text,
                });
                Navigator.pop(context);
              },
            )
          ],
        ),
      ),
    );
  }
}

我在堆栈溢出上搜索错误消息(红色屏幕和黄色文字):错误状态:DocumentSnapshotPlatform中不存在字段,但它找不到与我的代码相关的答案。请帮助?

xxls0lw8

xxls0lw81#

docs[index]是使用firebase扩展名.data()将文档快照转换为Map的文档快照〈string,dynamic〉

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

class ResponsePage extends StatefulWidget {
  @override
  _ResponsePageState createState() => _ResponsePageState();
}

class _ResponsePageState extends State<ResponsePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xFF006400),
        title: Text('Visualize Personal'),
      ),
      body: StreamBuilder(
        stream: FirebaseFirestore.instance.collection('personal').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }

          return ListView.builder(
            itemCount: snapshot.data!.docs.length,
            itemBuilder: (context, index) {
              final form = snapshot.data!.docs[index].data() as Map<String,dynamic>;

              return ListTile(
                title: Text(form['title']),
                onTap: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => DetailPage(form: form),
                    ),
                  );
                },
              );
            },
          );
        },
      ),
    );
  }
}

class DetailPage extends StatefulWidget {
  DetailPage({Key? key, required this.form}) : super(key: key);

  final DocumentSnapshot form;
  @override
  _DetailPageState createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  final TextEditingController _responseController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Detail Page')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // Display the data for the entry
            Text('Title: ${widget.form['title']}'),
            Text('Description: ${widget.form['description']}'),
            Text('Name: ${widget.form['name']}'),
            Text('Number: ${widget.form['number']}'),
            Text('Response: ${widget.form['response']}'),
            Text('Street: ${widget.form['street']}'),
            Text('Type: ${widget.form['type']}'),
            SizedBox(height: 16.0),
            // Add the text field and submit button
            TextField(
              controller: _responseController,
              decoration: InputDecoration(
                labelText: 'Response',
                border: OutlineInputBorder(),
              ),
            ),
            SizedBox(height: 8.0),
            ElevatedButton(
              child: Text('Submit'),
              onPressed: () {
                // Update the response field in the Firebase collection
                FirebaseFirestore.instance
                    .collection('your_collection')
                    .doc(widget.form.id)
                    .update({
                  'response': _responseController.text,
                });
                Navigator.pop(context);
              },
            )
          ],
        ),
      ),
    );
  }
}

相关问题