简体   繁体   中英

Using Title in Flutter

import 'package:flutter/material.dart';

class CategoryMealsScreen extends StatelessWidget {


  @override
  Widget build(BuildContext context) {
    final routeArgs =
        ModalRoute.of(context)!.settings.arguments as Map<String, String>;
    final categoryTitle = routeArgs['title'];
    final categoryId = routeArgs['id'];
    return Scaffold(
      appBar: AppBar(
        title: Text(categoryTitle),
      ),
      body: Center(
        child: Text(
          'The Recipes For The Category!',
        ),
      ),
    );
  }
}

I got error with this code and the error in title: Text(categoryTitle),what should i do to fix it?

you can convert it to string

Text(categoryTitle.toString());

You are facing this error because the type of categoryTitle might not be in String format. You can solve it by follwing ways:

  1. You can convert categoryTitle to String when you are first assigning like this:

final String categoryTitle = routeArgs['title'].toString();

  1. You can convert to String when you want to display in Widgets in the UI like this:
appBar: AppBar(
        title: Text(categoryTitle.toString()),
      ),

However, I would suggest to convert to the type you need while assigning it only so that you don't have to worry about type conversion later on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM