Reputation: 13
I'm trying to get data to a different .dart
file from Future
.
For doing this, I created a class after Future
code.
The code in pageone.dart:
class Popups extends StatefulWidget {
@override
_PopupsState createState() => _PopupsState();
}
class _PopupsState extends State<Popups> {
Future oyuncuSec() async {
await showDialog(
context: context,
...
...
}
}
class UserService {
oyuncuSec() {}
}
The code in pagetwo.dart, where I would like to call the same Future
code:
import 'package:myappname/pageone.dart';
.
.
UserService userService = UserService();
.
.
RaisedButton(
onPressed: () => userService.oyuncuSec(), child: Text('Futbolcu Resmi'),),
But, when I run the page, and press on the RaisedButton (Futbolcu Resmi), it does not show up the pop-up. I'm stick on this for days. Please help me.
Upvotes: 0
Views: 3336
Reputation: 3226
I see that you have created a separate class with the function definition inside it, that's the right way of doing it!
You must be missing something minimal in your code, make sure you define the class and the function as follows:
Let's say you are writing the class which has the function in a file called data.dart.
data.dart should look like this:
Class DataSource {
Future oyuncuSec() async {
await showDialog(
context: context,
...
...
}
}
Now let's say you want to use this function in a file called x.dart, this goes as follows:
in x.dart (which has a stateful widget), outside the build function:
var dataSource = new Datasource();
//an example function:
getData() async {
await dataSource.oyuncuSec(); //use a .then() in the end if the function oyuncuSec returns something and you want to do something with the result...
}
Edit: If you don't want oyuncuSec to return anything and just make a popup, there is no need to set its return type as Future. Just define it as void oyuncuSec() {//code for popup}
Upvotes: 3