我的颤振代码显示某个小部件被视为死代码,这是为什么?

xpe*_*ert 1 firebase flutter google-cloud-firestore

当我制作应用程序时,我的 IDE 出现错误,表明我的部分代码已失效。

唯一的问题是,我不知道为什么它会给我这个错误

我尝试重写代码,甚至去其他小部件查看代码,但问题仍然出现。这是受影响的代码的一部分。

!loading? Align(
                                alignment: Alignment.topLeft,
                                child: ElevatedButton(
                                    onPressed: () async {
                                      setState(() {
                                        loading = true;
                                      });
                                      final path = '${FirebaseAuth.instance.currentUser?.uid}/${widget.index}/${pickedfile!.name}';
                                      final ref = FirebaseStorage.instance.ref().child(path);
                                      uploadTask = ref.putData(pickedfile!.bytes!);
                                      await uploadTask!.whenComplete((){});
                                      final snapshot = uploadTask!.snapshot;
                                      final urlDownload = await snapshot.ref.getDownloadURL();
                                      await FirebaseFirestore.instance.collection("Users").doc("${FirebaseAuth.instance.currentUser?.uid}").collection("Videos").doc("Video ${widget.index}").set({
                                        pickedfile!.name : urlDownload
                                      });
                                    },
                                    child: Text("Activate")
                                )
                              ):/*This part is the dead code*/Align(
                                alignment: Alignment.topLeft,
                                child: CircularProgressIndicator(),
                              )
Run Code Online (Sandbox Code Playgroud)

以及有状态小部件的代码

class VideoGptDialog extends StatefulWidget {
  final int index;
  const VideoGptDialog({required this.index, Key? key}) : super(key: key);

  @override
  State<VideoGptDialog> createState() => _VideoGptDialogState();
}

class _VideoGptDialogState extends State<VideoGptDialog> {
  TextEditingController gptController = TextEditingController();
  PlatformFile? pickedfile;
  UploadTask? uploadTask;
  late VideoPlayerController _controller;
  @override
  Widget build(BuildContext context) {
    bool loading = false;
    return Dialog(
      backgroundColor: Colors.transparent,
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(20))
        ),
        child: SizedBox(
          width: 1000,
          height: 700,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Align(
                alignment: Alignment.topRight,
                child: IconButton(
                  icon: Icon(Icons.close),
                  onPressed: () {Navigator.pop(context);},
                ),
              ),
              Align(
                alignment: Alignment.center,
                child: Column(
                  children: [
                    Text(
                        "Short Clip a video",
                      style: TextStyle(
                        fontFamily: 'poppins',
                        fontSize: 24
                      ),
                    ),
                    SizedBox(height: 20,),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        InkWell(
                          onTap: () async{
                            final result = await FilePicker.platform.pickFiles(type: FileType.video);
                            if(result != null){
                              setState(() {
                                pickedfile = result.files.first;
                              });
                            }
                          },
                          child: Container(
                            width: 300,
                            height: 300,
                            decoration: BoxDecoration(
                                boxShadow: [BoxShadow()],
                                borderRadius: BorderRadius.all(Radius.circular(10)),
                                border: Border.all(color: Colors.blue),
                                color: Colors.white
                            ),
                            child: pickedfile == null? Icon(Icons.add, color: Colors.blue, size: 128,):Center(child: Text(pickedfile!.name.toString())),
                          ),
                        ),
                        SizedBox(width: 10,),
                        SizedBox(
                          width: 300,
                          height: 300,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: [
                              Align(
                                alignment: Alignment.topLeft,
                                child: Text("What would you like to extract"),
                              ),
                              SizedBox(height: 10,),
                              Align(
                                alignment: Alignment.topLeft,
                                child: TextField(
                                  controller: gptController,
                                  decoration: InputDecoration(
                                    border: OutlineInputBorder(
                                      borderSide: BorderSide(
                                        width: 3, color: Colors.black
                                      )
                                    )
                                  ),
                                ),
                              ),
                              !loading? Align(
                                alignment: Alignment.topLeft,
                                child: ElevatedButton(
                                    onPressed: () async {
                                      setState(() {
                                        loading = true;
                                      });
                                      final path = '${FirebaseAuth.instance.currentUser?.uid}/${widget.index}/${pickedfile!.name}';
                                      final ref = FirebaseStorage.instance.ref().child(path);
                                      uploadTask = ref.putData(pickedfile!.bytes!);
                                      await uploadTask!.whenComplete((){});
                                      final snapshot = uploadTask!.snapshot;
                                      final urlDownload = await snapshot.ref.getDownloadURL();
                                      await FirebaseFirestore.instance.collection("Users").doc("${FirebaseAuth.instance.currentUser?.uid}").collection("Videos").doc("Video ${widget.index}").set({
                                        pickedfile!.name : urlDownload
                                      });
                                    },
                                    child: Text("Activate")
                                )
                              ):/*This part is the dead code*/Align(
                                alignment: Alignment.topLeft,
                                child: CircularProgressIndicator(),
                              )
                            ],
                          ),
                        )
                      ],
                    ),
                  ],
                ),
              ),
              const SizedBox()
            ],
          ),
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

Dha*_*han 5

您在方法loading内部声明,这使得每次小部件重建时,build的值loading始终为该值。false

@override
Widget build(BuildContext context) {
  bool loading = false; // Don't do this
  // ...
Run Code Online (Sandbox Code Playgroud)

这是一种错误的用法StatefulWidget。将的声明loading移至State.

class _VideoGptDialogState extends State<VideoGptDialog> {
  bool loading = false;
  // ...
}
Run Code Online (Sandbox Code Playgroud)