无效的值:不在包容性范围0..4:5

发布于 2025-02-13 01:29:41 字数 7225 浏览 1 评论 0原文

我遇到了奇怪的错误。我想为列表中的每个单元格创建一个小部件,但是当我编写“ itemCount:smindtitle.length”时,我会有那个红色屏幕。 即使我只输入1、2或任何int编号,

第二个问题也是我有一个时间选择器,但是当我要输出输出的数字时,它仅显示随机数,例如0分钟,30hrs

import 'dart:io';
import 'package:flutter/material.dart';
    void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(debugShowCheckedModeBanner: false,
        home: HomePage());
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  var RemindTitle = ["First", "Second", "Third", "Fourth", "sdf"],
      RemindText = ["TexttF", "TextS", "TextT", "TextFr", "sdf"];
  var Counter = -1;
  DateTime? date;
  TimeOfDay _time = TimeOfDay.now();
  var DateText = "Enter the date", ChosenTime = " ";

  bool AlreadyBuilt = false;

  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
          padding: EdgeInsets.all(20),
          itemCount: RemindTitle.length,
          itemBuilder: (BuildContext context, int index) {
            if(AlreadyBuilt == false) {
              AlreadyBuilt = true;
              return Row(
                children: [
                Expanded(
                flex: 4,
                child: Container(
                  height: 75,
                  child: ElevatedButton(
                    child: Center(
                      child: Text("Add Remind"),
                    ),
                    onPressed: () {
                      showModalBottomSheet<void>(
                          context: context,
                          builder: (BuildContext context) {
                            return Container(
                                child: Center(
                                  child: Column(children: [
                                    Row(
                                      children: [
                                        Padding(padding: EdgeInsets.all(10)),
                                        Expanded(
                                            child: ElevatedButton(
                                                onPressed: () async {
                                                  date = await showDatePicker(
                                                    context: context,
                                                    initialDate:
                                                    new DateTime.now(),
                                                    firstDate:
                                                    new DateTime(2015),
                                                    lastDate:
                                                    new DateTime(2030),
                                                  );
                                                },
                                                child: Text("Enter the date"))),
                                        Padding(padding: EdgeInsets.all(10)),
                                        Expanded(
                                            child: ElevatedButton(
                                                onPressed: () async {
                                                  showTimePicker(
                                                      context: context,
                                                      initialTime:
                                                      TimeOfDay.now())
                                                      .then((value) {
                                                    setState(() {
                                                      _time = value!;
                                                    });
                                                  });
                                                  if (date != null) {
                                                    ChosenTime =
                                                        "Chosen time: " +
                                                            date!.day
                                                                .toString() +
                                                            ":" +
                                                            date!.month
                                                                .toString() +
                                                            ":" +
                                                            date!.year
                                                                .toString() +
                                                            ", time: " + _time.minute.toString() +
                                                            ":" +
                                                            _time.hour
                                                                .toString();
                                                  }
                                                },
                                                child: Text("Enter the time"))),
                                        Padding(padding: EdgeInsets.all(10)),
                                      ],
                                    ),
                                    Padding(padding: EdgeInsets.all(10)),
                                    Text(ChosenTime)
                                  ]),
                                ));
                          });
                    },
                  ),
                ),
              ),
            Padding(padding: EdgeInsets.fromLTRB(10, 0, 0, 0)),
            Expanded(
            flex: 4,
            child: Container(
            height: 75,
            child: ElevatedButton(
            onPressed: () {},
            child: Center(
            child: Text("Delete Remind"),
            )),
            ),
            )
            ],
            );
            }

            if(Counter < RemindTitle.length){
              Counter++;
            }
              return Container(
                height: 130,
                width: 175,
                child: ElevatedButton(
                    onPressed: () {},
                    child: Center(
                      child: Column(
                        children: [
                          Padding(padding: EdgeInsets.fromLTRB(0, 10, 0, 10)),
                          Text(
                            RemindTitle[Counter],
                            style: TextStyle(fontSize: 20),
                          ),
                          Padding(padding: EdgeInsets.fromLTRB(0, 10, 0, 10)),
                          Text(RemindText[Counter])
                        ],
                      ),
                    )),
                padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
              );
          }),
      appBar: AppBar(
        title: (Text("Reminder :)")),
        centerTitle: true,
      ),
    );
  }
}

红色屏幕

im getting strange error. I want to create a widget for every cell in a list, but when I write "itemCount: RemindTitle.length" I have that red screen.
It appears even when I enter just 1, 2 or any Int number

The second issue is that I have a Time Picker, but when I want to output numbers I've entered, it shows just random numbers, like 0 min, 30hrs

import 'dart:io';
import 'package:flutter/material.dart';
    void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(debugShowCheckedModeBanner: false,
        home: HomePage());
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  var RemindTitle = ["First", "Second", "Third", "Fourth", "sdf"],
      RemindText = ["TexttF", "TextS", "TextT", "TextFr", "sdf"];
  var Counter = -1;
  DateTime? date;
  TimeOfDay _time = TimeOfDay.now();
  var DateText = "Enter the date", ChosenTime = " ";

  bool AlreadyBuilt = false;

  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
          padding: EdgeInsets.all(20),
          itemCount: RemindTitle.length,
          itemBuilder: (BuildContext context, int index) {
            if(AlreadyBuilt == false) {
              AlreadyBuilt = true;
              return Row(
                children: [
                Expanded(
                flex: 4,
                child: Container(
                  height: 75,
                  child: ElevatedButton(
                    child: Center(
                      child: Text("Add Remind"),
                    ),
                    onPressed: () {
                      showModalBottomSheet<void>(
                          context: context,
                          builder: (BuildContext context) {
                            return Container(
                                child: Center(
                                  child: Column(children: [
                                    Row(
                                      children: [
                                        Padding(padding: EdgeInsets.all(10)),
                                        Expanded(
                                            child: ElevatedButton(
                                                onPressed: () async {
                                                  date = await showDatePicker(
                                                    context: context,
                                                    initialDate:
                                                    new DateTime.now(),
                                                    firstDate:
                                                    new DateTime(2015),
                                                    lastDate:
                                                    new DateTime(2030),
                                                  );
                                                },
                                                child: Text("Enter the date"))),
                                        Padding(padding: EdgeInsets.all(10)),
                                        Expanded(
                                            child: ElevatedButton(
                                                onPressed: () async {
                                                  showTimePicker(
                                                      context: context,
                                                      initialTime:
                                                      TimeOfDay.now())
                                                      .then((value) {
                                                    setState(() {
                                                      _time = value!;
                                                    });
                                                  });
                                                  if (date != null) {
                                                    ChosenTime =
                                                        "Chosen time: " +
                                                            date!.day
                                                                .toString() +
                                                            ":" +
                                                            date!.month
                                                                .toString() +
                                                            ":" +
                                                            date!.year
                                                                .toString() +
                                                            ", time: " + _time.minute.toString() +
                                                            ":" +
                                                            _time.hour
                                                                .toString();
                                                  }
                                                },
                                                child: Text("Enter the time"))),
                                        Padding(padding: EdgeInsets.all(10)),
                                      ],
                                    ),
                                    Padding(padding: EdgeInsets.all(10)),
                                    Text(ChosenTime)
                                  ]),
                                ));
                          });
                    },
                  ),
                ),
              ),
            Padding(padding: EdgeInsets.fromLTRB(10, 0, 0, 0)),
            Expanded(
            flex: 4,
            child: Container(
            height: 75,
            child: ElevatedButton(
            onPressed: () {},
            child: Center(
            child: Text("Delete Remind"),
            )),
            ),
            )
            ],
            );
            }

            if(Counter < RemindTitle.length){
              Counter++;
            }
              return Container(
                height: 130,
                width: 175,
                child: ElevatedButton(
                    onPressed: () {},
                    child: Center(
                      child: Column(
                        children: [
                          Padding(padding: EdgeInsets.fromLTRB(0, 10, 0, 10)),
                          Text(
                            RemindTitle[Counter],
                            style: TextStyle(fontSize: 20),
                          ),
                          Padding(padding: EdgeInsets.fromLTRB(0, 10, 0, 10)),
                          Text(RemindText[Counter])
                        ],
                      ),
                    )),
                padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
              );
          }),
      appBar: AppBar(
        title: (Text("Reminder :)")),
        centerTitle: true,
      ),
    );
  }
}

Red Screen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揪着可爱 2025-02-20 01:29:41

这是因为您的提示长度为5,您正在尝试访问第六项,而该项目不可用。时间也正确显示。但是,您首先显示会议分钟,而以24小时格式的小时和SETSTATE在模型中无法正常工作,显示您选择的任何时间,始终是您按下按钮的当前时间。

希望这能解决您的问题。

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
        debugShowCheckedModeBanner: false, home: const HomePage());
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<String> remindTitle = ["First", "Second", "Third", "Fourth", "sdf"],
      remindText = ["TexttF", "TextS", "TextT", "TextFr", "sdf"];
  int counter = -1;
  DateTime? date;
  TimeOfDay _time = TimeOfDay.now();
  String dateText = "Enter the date", chosenTime = "";
  bool alreadyBuilt = false;

  showDateTime() {
    if (date != null) {
      int hour = _time.hour > 12
          ? _time.hour - 12
          : _time.hour;
      String meridian =
      _time.hour > 12
          ? "PM"
          : "AM";
      chosenTime =
      "Chosen Date: ${date!.day}:${date!.month < 10 ? '0' : ''}${date!.month}:${date!.year}, time: ${hour < 10 ? '0' : ''}$hour:${_time.minute} $meridian";
      setState((){});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Container(
            padding: const EdgeInsets.fromLTRB(20, 20, 20, 0),
            height: 100,
            child: Row(
              children: [
                Expanded(
                  flex: 4,
                  child: SizedBox(
                    height: 75,
                    child: ElevatedButton(
                      child: const Center(
                        child: Text("Add Remind"),
                      ),
                      onPressed: () {
                        showModalBottomSheet<void>(
                            context: context,
                            builder: (BuildContext context) {
                              return Center(
                                child: Column(children: [
                                  Row(
                                    children: [
                                      const Padding(
                                          padding: EdgeInsets.all(10)),
                                      Expanded(
                                          child: ElevatedButton(
                                              onPressed: () async {
                                                date = await showDatePicker(
                                                  context: context,
                                                  initialDate: DateTime.now(),
                                                  firstDate: DateTime(2015),
                                                  lastDate: DateTime(2030),
                                                );
                                                showDateTime();
                                              },
                                              child: const Text(
                                                  "Enter the date"))),
                                      const Padding(
                                          padding: EdgeInsets.all(10)),
                                      Expanded(
                                          child: ElevatedButton(
                                              onPressed: () async {
                                                showTimePicker(
                                                        context: context,
                                                        initialTime:
                                                            TimeOfDay.now())
                                                    .then((value) {
                                                  _time = value!;
                                                  showDateTime();
                                                });
                                              },
                                              child: const Text(
                                                  "Enter the time"))),
                                      const Padding(
                                          padding: EdgeInsets.all(10)),
                                    ],
                                  ),
                                  const Padding(padding: EdgeInsets.all(10)),
                                  Text(chosenTime)
                                ]),
                              );
                            });
                      },
                    ),
                  ),
                ),
                const Padding(padding: EdgeInsets.fromLTRB(10, 0, 0, 0)),
                Expanded(
                  flex: 4,
                  child: SizedBox(
                    height: 75,
                    child: ElevatedButton(
                        onPressed: () {},
                        child: const Center(
                          child: Text("Delete Remind"),
                        )),
                  ),
                )
              ],
            ),
          ),
          Expanded(
            flex: 1,
            child: ListView.builder(
                padding: const EdgeInsets.all(20),
                itemCount: remindTitle.length,
                itemBuilder: (BuildContext context, int index) {
                  return Container(
                    height: 130,
                    width: 175,
                    padding: const EdgeInsets.fromLTRB(0, 20, 0, 0),
                    child: ElevatedButton(
                        onPressed: () {},
                        child: Center(
                          child: Column(
                            children: [
                              const Padding(
                                  padding: EdgeInsets.fromLTRB(0, 10, 0, 10)),
                              Text(
                                remindTitle[index],
                                style: const TextStyle(fontSize: 20),
                              ),
                              const Padding(
                                  padding: EdgeInsets.fromLTRB(0, 10, 0, 10)),
                              Text(remindText[index])
                            ],
                          ),
                        )),
                  );
                }),
          ),
        ],
      ),
      appBar: AppBar(
        title: (const Text("Reminder :)")),
        centerTitle: true,
      ),
    );
  }
}

This is because your remindTitle length is 5 and you are trying to access the 6th item which is not available. And Time is also showing correctly. But you are displaying minutes at first and the hour which is in 24 hours format and setState is not working correctly inside the model, show whatever time you choose, it is always the current time when you press the button.

Hope this solves your problem.

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
        debugShowCheckedModeBanner: false, home: const HomePage());
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<String> remindTitle = ["First", "Second", "Third", "Fourth", "sdf"],
      remindText = ["TexttF", "TextS", "TextT", "TextFr", "sdf"];
  int counter = -1;
  DateTime? date;
  TimeOfDay _time = TimeOfDay.now();
  String dateText = "Enter the date", chosenTime = "";
  bool alreadyBuilt = false;

  showDateTime() {
    if (date != null) {
      int hour = _time.hour > 12
          ? _time.hour - 12
          : _time.hour;
      String meridian =
      _time.hour > 12
          ? "PM"
          : "AM";
      chosenTime =
      "Chosen Date: ${date!.day}:${date!.month < 10 ? '0' : ''}${date!.month}:${date!.year}, time: ${hour < 10 ? '0' : ''}$hour:${_time.minute} $meridian";
      setState((){});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Container(
            padding: const EdgeInsets.fromLTRB(20, 20, 20, 0),
            height: 100,
            child: Row(
              children: [
                Expanded(
                  flex: 4,
                  child: SizedBox(
                    height: 75,
                    child: ElevatedButton(
                      child: const Center(
                        child: Text("Add Remind"),
                      ),
                      onPressed: () {
                        showModalBottomSheet<void>(
                            context: context,
                            builder: (BuildContext context) {
                              return Center(
                                child: Column(children: [
                                  Row(
                                    children: [
                                      const Padding(
                                          padding: EdgeInsets.all(10)),
                                      Expanded(
                                          child: ElevatedButton(
                                              onPressed: () async {
                                                date = await showDatePicker(
                                                  context: context,
                                                  initialDate: DateTime.now(),
                                                  firstDate: DateTime(2015),
                                                  lastDate: DateTime(2030),
                                                );
                                                showDateTime();
                                              },
                                              child: const Text(
                                                  "Enter the date"))),
                                      const Padding(
                                          padding: EdgeInsets.all(10)),
                                      Expanded(
                                          child: ElevatedButton(
                                              onPressed: () async {
                                                showTimePicker(
                                                        context: context,
                                                        initialTime:
                                                            TimeOfDay.now())
                                                    .then((value) {
                                                  _time = value!;
                                                  showDateTime();
                                                });
                                              },
                                              child: const Text(
                                                  "Enter the time"))),
                                      const Padding(
                                          padding: EdgeInsets.all(10)),
                                    ],
                                  ),
                                  const Padding(padding: EdgeInsets.all(10)),
                                  Text(chosenTime)
                                ]),
                              );
                            });
                      },
                    ),
                  ),
                ),
                const Padding(padding: EdgeInsets.fromLTRB(10, 0, 0, 0)),
                Expanded(
                  flex: 4,
                  child: SizedBox(
                    height: 75,
                    child: ElevatedButton(
                        onPressed: () {},
                        child: const Center(
                          child: Text("Delete Remind"),
                        )),
                  ),
                )
              ],
            ),
          ),
          Expanded(
            flex: 1,
            child: ListView.builder(
                padding: const EdgeInsets.all(20),
                itemCount: remindTitle.length,
                itemBuilder: (BuildContext context, int index) {
                  return Container(
                    height: 130,
                    width: 175,
                    padding: const EdgeInsets.fromLTRB(0, 20, 0, 0),
                    child: ElevatedButton(
                        onPressed: () {},
                        child: Center(
                          child: Column(
                            children: [
                              const Padding(
                                  padding: EdgeInsets.fromLTRB(0, 10, 0, 10)),
                              Text(
                                remindTitle[index],
                                style: const TextStyle(fontSize: 20),
                              ),
                              const Padding(
                                  padding: EdgeInsets.fromLTRB(0, 10, 0, 10)),
                              Text(remindText[index])
                            ],
                          ),
                        )),
                  );
                }),
          ),
        ],
      ),
      appBar: AppBar(
        title: (const Text("Reminder :)")),
        centerTitle: true,
      ),
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文