是否可以在flutter中不需要传递参数

发布于 2025-01-11 12:57:02 字数 719 浏览 0 评论 0原文

是否可以在没有必需的情况下传递 this.title ,我看过他们这样做的一些教程,但是当我尝试时,它要求我将必需添加到 this.title 。由于我的 2 屏幕不包含 appBar 标题,我想在不需要的情况下传递它。是否可以?

如果我将 required 添加到 this.title ,我的代码可以正常工作。

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

import '../../logic/cubit/counter_cubit.dart';

class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key,  this.title}) : super(key: key);

final String title;

@override
State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
[SS of code][1]

is it possible to pass this.title without required, i have seen some tutorial they doing so, but when i try it asks me to add required to this.title. As my 2 screen doesn't contain appBar title i want to pass it without required. is it possible?

here my code works fine if i add required to this.title.

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

import '../../logic/cubit/counter_cubit.dart';

class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key,  this.title}) : super(key: key);

final String title;

@override
State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
[SS of code][1]

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

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

发布评论

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

评论(2

冷情 2025-01-18 12:57:03

标题必须是必需,因为您将其设置为不可可为空。如果标题是可选的,您可以将其设置为可为空:

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key, this.title}) : super(key: key);

  final String? title;

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

但是,现在您需要处理您的 build 函数中可能没有标题的事实:

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title ?? "NO TITLE!?!"),
      ),
     );
  }
}

显然您没有不想“没有标题!?!”出现在你的标题中,但由于我不知道你想要什么,所以你必须更改该部分。

The title needs to be required because you made it not nullable. You can make it nullable if the title is optional:

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key, this.title}) : super(key: key);

  final String? title;

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

However, now you need to handle the fact that you may not have a title in your build function:

class _HomeScreenState extends State<HomeScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title ?? "NO TITLE!?!"),
      ),
     );
  }
}

Obviously you don't want "NO TITLE!?!" to be in your title, but since I don't know what you want instead, you will have to change that part.

生死何惧 2025-01-18 12:57:03

除了@nvoigt给出的答案之外,您还可以提供一个默认值,而不是使 title 可以为空:

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key, this.title = 'Home Screen'}) : super(key: key);

  final String title;

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

这样,您就不必提供标题,如果您不提供,它将使用主屏幕作为标题。

In addition to the answer given by @nvoigt, you can also supply a default value instead of making title nullable:

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key, this.title = 'Home Screen'}) : super(key: key);

  final String title;

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

This way, you don't have to supply a title, and if you don't, it will use Home Screen as the title instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文