Skip to content

Instantly share code, notes, and snippets.

View flutter_view1.dart
SingleChildScrollView(
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 48),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
_currentMovie.title.replaceAll(" ", "\n").toUpperCase(),
style: TextStyle(
shadows: [
Shadow(
View MainActivity_channel.kt
//On Android
class MainActivity() : FlutterActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
MethodChannel(flutterView, "samples.flutter.io/battery").setMethodCallHandler { call, result ->
if (call.method == "getBatteryLevel") {
val batteryLevel = getBatteryLevel()
View flutter_channel.dart
//From Flutter
Future<String> _getBatteryLevel() async {
String batteryLevel;
try {
//invoke the platform specific code
//wait for the result
final int result = await platform.invokeMethod('getBatteryLevel');
batteryLevel = 'Battery level at $result % .';
} on PlatformException catch (e) {
batteryLevel = "Failed to get battery level: '${e.message}'.";
View SearchViewModel.kt
class SearchViewModel(application: Application) : AndroidViewModel(application), CoroutineScope {
private val job = Job()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
private val mutableViewState = MutableLiveData<SearchViewState>()
val viewState : LiveData<SearchViewState> = mutableViewState
private val repository by lazy {
mainApplication.omdbRepository
View OmdbRepository.java
interface OMDBApi {
@GET("/")
fun searchMovies(@Query("s") name: String, @Query("apiKey") apikey: String) : Deferred<SearchResponse>
companion object {
val apiKey = "YOUR_KEY"
val baseUrl = "http://www.omdbapi.com/"
}
}
View Movie.java
class Movie {
@SerializedName("Title")
var title : String = ""
@SerializedName("Year")
var year : String = ""
@SerializedName("ImdbID")
var imdbID : String = ""
@SerializedName("Type")
var type : String = ""
@SerializedName("Poster")
View Movie.dart
part 'Movie.g.dart';
@JsonSerializable()
class Movie {
@JsonKey(name: "Title")
final String title;
@JsonKey(name: "Year")
final String year;
@JsonKey(name: "ImdbID")
final String imdbID;
View OmdbRepository.dart
class OmdbRepository {
final apiKey = "my_key";
const OmdbRepository();
Future<List<Movie>> searchMovies(String byName) async {
final response = await http.get('http://www.omdbapi.com/?s=$byName&apikey=$apiKey');
final jsonValue = json.decode(response.body);
return SearchResponse.fromJson(jsonValue).movies;
}
}
View SearchViewModel.dart
class SearchViewModel {
//Stream <=> MediatorLiveData
final StreamController<SearchViewState> _viewModelController = StreamController();
final OmdbRepository _repository;
//Stream <=> LiveData
Stream<SearchViewState> get viewState => _viewModelController.stream;
//constructor
SearchViewModel(this._repository) {
View molki.java
User use = molki(User.class)
.given(user -> user.getName()).willReturn(3)
.given(user -> user.getFirstName()).willReturn(4)