Bloc: BlocProvider does not work from in GestureDetector onTap

Created on 18 Aug 2020  路  2Comments  路  Source: felangel/bloc

Hello, I got unexpected behavior with bloc where I want to navigate to Filter page when user taps on Sort and pass productsbloc to it

here is sample code:
```import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Home(),
);
}
}

class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.only(top: 58.0),
child: GestureDetector(
onTap: () {
BlocProvider(
create: (context) => ProductsBloc(),
child: Filter(),
);
},
child: Center(child: Icon(Icons.sort)),
),
),
);
}
}

class Filter extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Text('data'),
);
}
}

class ProductsBloc extends Bloc {
ProductsBloc() : super(InitialState());

@override
Stream mapEventToState(event) {
// TODO: implement mapEventToState
throw UnimplementedError();
}
}

class ProductsEvent {}

class ProductsState {}

class InitialState extends ProductsState {}
```

question

Most helpful comment

Hi @EngAddow 馃憢
Thanks for opening an issue!

It doesn't appear you are navigating and it looks like you're just creating a new bloc and Filter widget. I think you should do something like:

onTap: () {
  Navigator.of(context).push(
    MaterialPageRoute(
      builder: (_) => BlocProvider(create: (_) => ProductsBloc(), child: Filter()),
    ),
  );
}

Closing for now but feel free to comment if you still have questions and I'm happy to continue the conversation 馃憤

All 2 comments

Hi @EngAddow 馃憢
Thanks for opening an issue!

It doesn't appear you are navigating and it looks like you're just creating a new bloc and Filter widget. I think you should do something like:

onTap: () {
  Navigator.of(context).push(
    MaterialPageRoute(
      builder: (_) => BlocProvider(create: (_) => ProductsBloc(), child: Filter()),
    ),
  );
}

Closing for now but feel free to comment if you still have questions and I'm happy to continue the conversation 馃憤

Thanks @felangel I appreciated for your time and how you reply quickly and very helpful even when the issue is not yours

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rsnider19 picture rsnider19  路  3Comments

clicksocial picture clicksocial  路  3Comments

komapeb picture komapeb  路  3Comments

abinvp picture abinvp  路  3Comments

RobPFarley picture RobPFarley  路  3Comments