Yakub Pasha
Yakub Pasha

Reputation: 533

How to get the Widget Class name from the builder object in MaterialPageRoute

I would like to have a custom MaterialPageRoute where the RouteSettings object would be assigned with a default name which should be the builder i.e. widget class name.

I have overrides the method as below and don't understand how to get the class name form the builder object.

import 'package:flutter/material.dart';
class CMaterialPageRoute extends MaterialPageRoute {


 CMaterialPageRoute({@required builder, RouteSettings settings, maintainState = true, bool fullscreenDialog = false,})
  : assert(builder != null),
    assert(maintainState != null),
    assert(fullscreenDialog != null),
    assert(opaque),
    super(settings: settings == null ? new RouteSettings(name: (builder as Widget).toStringShort()):settings,fullscreenDialog: fullscreenDialog);

}

in the above code i was trying to get the Widget from the builder and set the short name as the RouteSettings Name but the problem is Casting and getting the below error says the cast is not possible

type '(dynamic) => CitySelectionScreen' is not a subtype of type 'Widget' in type cast

The reason why I am doing this is as part of the Firebase analytics integration, I would require to change my whole navigation code to add the new RouteSettings which was not there before and i was trying to do work around, to set the default name instead of the changes to be done at the complete project level.

Please help me in this regard.

Upvotes: 0

Views: 1604

Answers (1)

Igor Kharakhordin
Igor Kharakhordin

Reputation: 9903

builder is a function of WidgetBuilder type and you can't cast it to Widget. I'm not sure what you're trying to achieve, but maybe if you call builder with null you'll be able to know which Widget it returns:

final WidgetBuilder b = (BuildContext context) => Scaffold();
print(b(null).toString());

It prints

Scaffold

Upvotes: 1

Related Questions