Closed maylortaylor closed 3 years ago
Hey there, Matt.
Just noticed your posting. I'll take a look and see if I can help you out here. Excellent you've provided a 'working program' it seems. Likely to have an answer in a bit.
Cheers
ok,
Matt, you're using the Singleton pattern on the Controller when one uses a factory constructor on it. That means when you instantiate the class, again and again, you'll only get the one instance that came about from the first instantiation only.
What you're doing wrong, however, is that you forgot to instantiate the Controller class in the first place. Check out the class, _MyHomePageState , below.
Also check out the sample code, counter_app, for my most recent Medium article, MVC Design Pattern in Flutter. It builds on the mvc_pattern to provide a full MVC framework called, mvc_application. It's a framework that works with the Flutter framework and may get you further along that learning curve.
Greg.
import 'package:flutter/material.dart';
import 'package:mvc_pattern/mvc_pattern.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
// Fields in a Widget subclass are always marked "final".
final String title = 'Flutter Demo Home Page';
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final Controller _con = Controller();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
widget.title,
),
Text(
'${Controller.con.counter}',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(_con.incrementCounter);
},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class Controller extends ControllerMVC {
/// Singleton Factory
factory Controller() => _this ??= Controller._();
Controller._();
static Controller _this;
/// Allow for easy access to 'the Controller' throughout the application.
static Controller get con => _this;
// int get counter => _counter;
int get counter => Model.counter;
void incrementCounter() {
/// The Controller knows how to 'talk to' the Model. It knows the name, but Model does the work.
Model.incrementCounter();
}
}
class Model {
static int get counter => _counter;
static int _counter = 0;
static int incrementCounter() => ++_counter;
}
I just wanted to drop in and say that I'm having difficulties as well reproducing runnable code for both the examples in your README file.
I'm very new to Flutter / Dart so perhaps more experienced people see the problem right away. However it would make sense to provide examples that can actually work.
Anyway thanks for guidance, this plugin is appreciated.
Sorry to hear you are having issues. I will have time to review those README examples this weekend. Until then, would you use your IDE's debugger and present me with a screenshot of the offending code? Maybe a little description of what you think may be the problem would be most helpful as well. Possibly, I can determine the issue for you before the end of the week.
Greg
I am trying to get a basic app started with this mvc_pattern plugin but every time I try, my controller object is null. With the code below, I get error "The getter 'counter' was called on null" at the line where I call this
_con.counter
in a Text Widget.What am I doing wrong? using mvc_pattern: ^6.6.0 and using Flutter 1.23.0-18.1.pre Main.dart
Controller.dart
Model.dart