Home > Software engineering >  Using Title in Flutter
Using Title in Flutter

Time:01-30

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?

CodePudding user response:

you can convert it to string

Text(categoryTitle.toString());

CodePudding user response:

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.

  • Related