Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigator pop parameter can not be String #87262

Closed
hongwei78 opened this issue Jul 29, 2021 · 5 comments
Closed

Navigator pop parameter can not be String #87262

hongwei78 opened this issue Jul 29, 2021 · 5 comments
Labels
r: invalid Issue is closed as not valid

Comments

@hongwei78
Copy link

Navigator.of(context).pop('cart');
It's not worked.

Expected results:
page closed

Actual results:
Unhandled Exception: type 'String' is not a subtype of type 'bool?' of 'result'
Navigator pop not work

[VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: type 'String' is not a subtype of type 'bool?' of 'result'
#0 LocalHistoryRoute.didPop (package:flutter/src/widgets/routes.dart)
#1 _RouteEntry.pop
package:flutter/…/widgets/navigator.dart:3085
#2 NavigatorState.pop
package:flutter/…/widgets/navigator.dart:5077
#3 GoodsDetailPage.detailBottom..
package:igocctv/…/goods/goodsdetail_page.dart:391
#4 GoodsDetailPage.detailBottom..
package:igocctv/…/goods/goodsdetail_page.dart:388
#5 _InkResponseState._handleTap
package:flutter/…/material/ink_well.dart:989
#6 GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:182
#7 TapGestureRecognizer.handleTapUp
package:flutter/…/gestures/tap.dart:607
#8 BaseTapGestureRecognizer._checkUp
package:flutter/…/gestures/tap.dart:296
#9 BaseTapGestureRecognizer.<…>

Flutter 2.2.3
flutter/packages/flutter/lib/src/widgets/routes.dart
line 636: bool didPop(T? result) {
T's type is bool,is not String

@maheshj01 maheshj01 added the in triage Presently being triaged by the triage team label Jul 29, 2021
@maheshj01
Copy link
Member

maheshj01 commented Jul 29, 2021

Hi @hongwei78,
Thanks for filing the issue, I believe you are trying to return a string on pop, If so could you please try this code sample

code sample
import 'package:flutter/material.dart';
import 'dart:async';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(primarySwatch: Colors.blue),
        home: Popview(title: 'title'));
    //  MyHomePage(title: 'App title'));
  }
}

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

  final String title;
  @override
  _PopviewState createState() => _PopviewState();
}

class _PopviewState extends State<Popview> {
  String stringFromActivity = 'Send Data on Pop';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              stringFromActivity,
              style: const TextStyle(fontSize: 20.0),
              textAlign: TextAlign.center,
            ),
            Container(
              height: 20.0,
            ),
            ElevatedButton(
              child: const Text('tap me'),
              onPressed: () => _push(),
            )
          ],
        ),
      ),
    );
  }

  Future _push() async {
    String results = await Navigator.of(context)
        .push(MaterialPageRoute(builder: (BuildContext context) {
      return SecondScreen();
    }));

    if (results != null) {
      setState(() {
        print('Data received on pop $results');
      });
    }
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Selecte Smily'),
      ),
      body: ListView.builder(
        itemBuilder: (context, i) {
          return ListTile(
            title: Text('item $i'),
            onTap: () {
              Navigator.of(context).pop('SEND THIS');
            },
          );
        },
        itemCount: 20,
      ),
    );
  }
}

if you are still facing the issue, then please provide the output of flutter doctor -v along with minimal reproducible code sample, This helps to better investigate the issue.

Thank you.

@maheshj01 maheshj01 added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Jul 29, 2021
@navaronbracke
Copy link
Contributor

navaronbracke commented Jul 29, 2021

NavigatorState.pop() has a type parameter. Did you provide String as the type parameter? According to the error, the type is bool (pop() takes a nullable type, hence the error reports bool?)

@HansMuller HansMuller changed the title Navigator pop parament can not be String Navigator pop parameter can not be String Jul 29, 2021
@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Aug 2, 2021
@hongwei78
Copy link
Author

hongwei78 commented Aug 2, 2021

Hi @maheshmnj
Thank you for your reply.
I tried your sample code is worked, but I need rewirte router, so I change the code sample and the issue can be repeated.

code sample
import 'package:flutter/cupertino.dart';

import 'package:flutter/material.dart';

import 'dart:async';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        onGenerateRoute: Router.generateRoute,
        initialRoute: RouteName.first,
        theme: ThemeData(primarySwatch: Colors.blue),
        home: Popview(title: 'title'));
    //  MyHomePage(title: 'App title'));
  }
}

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

  final String title;
  @override
  _PopviewState createState() => _PopviewState();
}

class _PopviewState extends State<Popview> {
  String stringFromActivity = 'Send Data on Pop';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              stringFromActivity,
              style: const TextStyle(fontSize: 20.0),
              textAlign: TextAlign.center,
            ),
            Container(
              height: 20.0,
            ),
            ElevatedButton(
              child: const Text('tap me'),
              onPressed: () => _push(),
            )
          ],
        ),
      ),
    );
  }

  Future _push() async {
    Object? results = await Navigator.of(context).pushNamed(RouteName.second);

    if (results != null) {
      setState(() {
        print('Data received on pop $results');
      });
    }
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Selecte Smily'),
      ),
      body: ListView.builder(
        itemBuilder: (context, i) {
          return ListTile(
            title: Text('item $i'),
            onTap: () {
              Navigator.of(context).pop('SEND THIS');
            },
          );
        },
        itemCount: 20,
      ),
    );
  }
}

class RouteName {
  static const String first = 'first';
  static const String second = 'second';
}

class Router {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
      case RouteName.first:
        return CupertinoPageRoute<bool>(
            settings: RouteSettings(name: 'first'),
            builder: (_) => Popview(
                  title: 'first',
                ));
      case RouteName.second:
        return CupertinoPageRoute<bool>(
            settings: RouteSettings(name: settings.name),
            builder: (_) => SecondScreen());
      default:
        return CupertinoPageRoute<bool>(
            settings: RouteSettings(name: settings.name),
            builder: (_) => Scaffold(
                  body: Center(
                    child: Text('No route defined for ${settings.name}'),
                  ),
                ));
    }
  }
}

/// Pop路由
class PopRoute extends PopupRoute {
  PopRoute({required this.child});

  Widget child;

  final Duration _duration = Duration(milliseconds: 300);

  @override
  Color? get barrierColor => null;

  @override
  bool get barrierDismissible => true;

  @override
  String? get barrierLabel => null;

  @override
  Duration get transitionDuration => _duration;

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    return child;
  }
}

@maheshj01
Copy link
Member

@hongwei78 thanks for the complete reproducible code I checked your code the issue is that the type is mismatched

You are expecting the CupertinoPageRoute to take bool here <bool>

return CupertinoPageRoute<bool>(
            settings: RouteSettings(name: 'first'),
            builder: (_) => Popview(
                  title: 'first',
                ));

And while popping the widget you are passing a String

onTap: () {
              Navigator.of(context).pop('SEND THIS'); /// either pass a boolean value or changing the type in CupertinoPageRoute to String.
            },

You have to make sure both types are the same. Feel free to comment if you need any further clarification, Closing this issue as invalid if you disagree please comment with your reasoning and I will reopen it.
Thank you.

@maheshj01 maheshj01 added r: invalid Issue is closed as not valid and removed in triage Presently being triaged by the triage team labels Aug 2, 2021
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
r: invalid Issue is closed as not valid
Projects
None yet
Development

No branches or pull requests

3 participants