Arun
Arun

Reputation: 3

In flutter, NavigationRail.destinations[0].label.toString() returns 'Text{"Text")' instead of "Text"

I use NavigationRail & NavigationBar in my project. When I try to read the label of one of the destinations attached to NavigationRail, I get Text("Text") instead of "Text".

Here is an example code:

onDestinationSelected: (value) {
  context.goNamed(destinations[value].label.toString()); // output is "Text("Text")"
},

Is this an issue?

I tried same logic on NavigationBar and it works fine.

destinations[value].label.toString(); // Output is "Text"

Upvotes: 0

Views: 52

Answers (2)

Nitin Bhalala
Nitin Bhalala

Reputation: 28

To resolve this, you need to access the actual text content within the label property. Here's how you can do it:

onDestinationSelected: (value) {
  context.goNamed(destinations[value].label.data.first.toString()); 
// Access the first text node
},       

Upvotes: 0

Reza Farjam
Reza Farjam

Reputation: 1150

You need to cast the label as a Text widget before accessing its data.
Here's how you can do it:

onDestinationSelected: (value) {
  context.goNamed((destinations[value].label as Text).data!);
}

The label property is a Text widget, not a String, so casting it first allows you to access the text content.

Upvotes: 1

Related Questions